20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

374 Chapter 17 Miscellaneous and Advanced Features<br />

Programmers who are lazy frequently abuse the goto statement to branch to other portions<br />

of their code.The goto statement <strong>in</strong>terrupts the normal sequential flow of a program.<br />

As a result, programs are harder to follow. Us<strong>in</strong>g many gotos <strong>in</strong> a program can<br />

make it impossible to decipher. For this reason, goto statements are not considered part<br />

of good programm<strong>in</strong>g style.<br />

The null Statement<br />

C permits a solitary semicolon to be placed wherever a normal program statement can<br />

appear.The effect of such a statement, known as the null statement, is that noth<strong>in</strong>g is<br />

done. Although this might seem useless, it is often used by C programmers <strong>in</strong> while,<br />

for,and do statements. For example, the purpose of the follow<strong>in</strong>g statement is to store<br />

all the characters read <strong>in</strong> from the standard <strong>in</strong>put <strong>in</strong>to the character array po<strong>in</strong>ted to by<br />

text until a newl<strong>in</strong>e character is encountered.<br />

while ( (*text++ = getchar ()) != '\n' )<br />

;<br />

All of the operations are performed <strong>in</strong>side the loop<strong>in</strong>g-conditions part of the while<br />

statement.The null statement is needed because the compiler takes the statement that<br />

follows the loop<strong>in</strong>g expression as the body of the loop.Without the null statement,<br />

whatever statement that follows <strong>in</strong> the program is treated as the body of the program<br />

loop by the compiler.<br />

The follow<strong>in</strong>g for statement copies characters from the standard <strong>in</strong>put to the standard<br />

output until the end of file is encountered:<br />

for ( ; (c = getchar ()) != EOF; putchar (c) )<br />

;<br />

The next for statement counts the number of characters that appear <strong>in</strong> the standard<br />

<strong>in</strong>put:<br />

for ( count = 0; getchar () != EOF; ++count )<br />

;<br />

As a f<strong>in</strong>al example illustrat<strong>in</strong>g the null statement, the follow<strong>in</strong>g loop copies the character<br />

str<strong>in</strong>g po<strong>in</strong>ted to by from to the one po<strong>in</strong>ted to by to.<br />

while ( (*to++ = *from++) != '\0' )<br />

;<br />

The reader is advised that there is a tendency among certa<strong>in</strong> programmers to try to<br />

squeeze as much as possible <strong>in</strong>to the condition part of the while or <strong>in</strong>to the condition<br />

or loop<strong>in</strong>g part of the for.Try not to become one of those programmers. In general,<br />

only those expressions <strong>in</strong>volved with test<strong>in</strong>g the condition of a loop should be <strong>in</strong>cluded<br />

<strong>in</strong>side the condition part. Everyth<strong>in</strong>g else should form the body of the loop.The only<br />

case to be made for form<strong>in</strong>g such complex expressions might be one of execution efficiency.<br />

Unless execution speed is that critical, you should avoid us<strong>in</strong>g these types of<br />

expressions.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!