Programming in C  © Kishori Mundargi; contributors: Myriam Grandchamp, Oscar Serrano Serrano, anisa chaudhary Source: PEOI

 

CHAPTER 15:

Miscellaneous

This chapter contains a number of miscellaneous topics of the C programming language.

The GOTO and EXIT statements

In lower level programming languages, e.g. assembler or BASIC, the goto statement is regularly used. In C there's no real need to use this functionality, however it is supported.

The syntax of the goto statement is:

goto identifier;

The point where the program should "goto" has to be labelled with the identifier. This is illustrated with the following example:

goto Label1;

.....

.....

Label1: x=y+1;

....

....

It has to be noted that C also supports the empty statement or null statement, which only consists of a semicolon (;). This makes it possible to jump to the end of a compound statement:

{ .....;

goto Label2;

.....

Label2: ;

}

Because a goto statement can only jump within a function, it is not possible to jump from a supporting function to the main function. Since there is a need to end the execution of a program in a simple way, the function exit() is available. This is usually used as follows:

exit(0);

The argument of the exit() function can be used by another process to "see" what causes the termination. Usually "0" means that everything was OK. Other exit situations can be indicated with specific values, e.g. exit(1). The values of the argument are application and system dependent. The important thing to know is that a program can be stopped with the exit function under our control. Ongoing I/O activities will be finished before termination.

Command line Argument ARGC and ARGV

Sometimes it can be useful to pass parameters to a program, for instance the input and output file names. This can be done via the arguments ARGC and ARGV.

  Review questions

  Assignments

Review questions

Quiz

[Your opinion is important to us. If you have a comment, correction or question pertaining to this chapter please send it to comments@peoi.org .]

Prior: C Preprocessor Modified: 2014-05-23 Next: Foreword