Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer app03.tex V1 - 09/04/2008 6:11pm Page 1208

Appendix C: Notes on C


Only the first line is included in theifbody. The remaining lines are executed regardless ofcondition
and this is not, of course, what was intended. Because structurally thedoconstruction counts as a state-
ment, it ensures thatallstatements included in the macro are placed within theifbody.

Further elements that cause some confusion when reading kernel sources are the C statementsbreakand
continue. The following chunks of code can easily be confused:

unsigned int count;
for (count = 0; count < 5; count++) {
if (count == 2) {
continue;
}
printf("count: %u\n", count);
}

The code produces the following output when executed:

wolfgang@meitner>./continue
count: 0
count: 1
count: 3
count: 4

The third loop pass is exited prematurely because of thecontinuestatement. Nevertheless, the subse-
quent loop passes are still executed.

Ifcontinueis replaced with abreakstatement as shown in the following code, program behavior is
modified:

unsigned int count;
for (count = 0; count < 5; count++) {
if (count == 2) {
break;
}
printf("count: %u\n", count);
}

Program output is now as follows:

wolfgang@meitner>./break
count: 0
count: 1

Again, the third loop pass is terminated. However, loop processing is not resumed, and the subsequent
code is executed. In other words,breakcompletely terminates the loop.

A further stumbling block in C are the semantics ofselectqueries, as the following example shows:

int var = 3;
switch (var) {
case 1:
Free download pdf