Miscellaneous Language Statements in C Programming Language

This section discusses two statement you haven’t encountered to this point: the goto and the null statement.

1. The goto Statement

Anyone who has learned about structured programming knows of the bad reputation afforded to the goto statement.Virtually every computer language has such a statement.

Execution of a goto statement causes a direct branch to be made to a specified point in the program. This branch is made immediately and unconditionally upon execution of the goto. To identify where in the program the branch is to be made, a label is needed. A label is a name that is formed with the same rules as variable  names and must be imme- diately followed by a colon. The label is placed directly before the statement to which the branch is to be made and must appear in the same function as the goto.

So, for example, the statement

goto out_of_data;

causes the program to branch immediately to the statement that is preceded by the label out_of_data:. This label can be located anywhere in the function, before or after the goto, and might be used as shown:

out_of_data: printf (“Unexpected end of data.\n”);

Programmers who are lazy frequently abuse the goto statement to branch to other por- tions of their code. The goto statement interrupts the normal sequential flow of a pro- gram. As a result, programs are harder to follow. Using many gotos in a program can make it impossible to decipher. For this reason, goto statements are not considered part of good programming style.

2. The null Statement

C permits a solitary semicolon to be placed wherever a normal program statement can appear. The effect of such a statement, known as the null statement, is that nothing is done. Although this might seem useless, it is often used by C programmers in while, for, and do statements. For example, the purpose of the following statement is to store all the characters read in from the standard input into the character array pointed to by text until a newline character is encountered.

while ( (*text++ = getchar ()) != ‘\n’ )

;

All of the operations are performed inside the looping-conditions part of the while statement. The null statement is needed because the compiler takes the statement that follows the looping expression  as the body of the loop. Without the null statement, whatever statement that follows in the program is treated as the body of the program loop by the compiler.

The following for statement copies characters from the standard input to the stan- dard output until the end of file is encountered:

for ( ; (c = getchar ()) != EOF; putchar (c) )

;

The next for statement counts the number of characters that appear in the standard input:

for ( count = 0; getchar () != EOF; ++count )

;

As a final example illustrating the null statement, the following loop copies the charac- ter string pointed to by from to the one pointed to by to.

while ( (*to++ = *from++) != ‘\0’ )

;

The reader is advised that there is a tendency among certain programmers to try to squeeze as much as possible  into the condition part of the while or into the condition or looping part of the for. Try not to become one of those programmers. In general, only those expressions involved with testing the condition of a loop should be included inside the condition part. Everything else should form the body of the loop. The only case to be made for forming such complex expressions might be one of execution effi- ciency. Unless execution speed is that critical, you should avoid using these types of expressions.

The preceding while statement is easier to read when written like this:

while ( *from != ‘\0’ )

*to++ = *from++;

*to = ‘\0’;

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *