C’s Dustier Corners - Advanced Programming Concepts - Practical C Programming, 3rd Edition (2011)

Practical C Programming, 3rd Edition (2011)

Part III. Advanced Programming Concepts

Chapter 21. C’s Dustier Corners

There be of them that have left a name behind them.

—Ecclesiasticus 44:8

This chapter describes the few remaining features of C that have not been described in any of the previous chapters. It is titled C’s Dustier Corners because these statements are hardly ever used in real programming.


The do/while statement has the following syntax:

do {statement


} while (expression);

The program will loop, test the expression, and stop if the expression is false (0).


This construct will always execute at least once.

do/while is not frequently used in C. Most programmers prefer to use a while/break combination.


Every sample program in this book was coded without using a single goto. In actual practice, I find a goto statement useful about once every other year.

For those rare times that a goto is necessary, the correct syntax is:


where label is a statement label. Statement labels follow the same naming convention as variable names. Labeling a statement is done as follows:


For example:

for (x = 0; x < X_LIMIT; x++) {

for (y = 0; y < Y_LIMIT; y++) {

if (data[x][y] == 0)

goto found;



printf("Not found\n");



printf("Found at (%d,%d)\n", x, y);

Question 21-1: Why does Example 21-1 not print an error message when an incorrect command is entered?

Hint: We put this in the goto section. (Click here for the answer Section 21.6)

Example 21-1. def/def.c

#include <stdio.h>

#include <stdlib.h>

int main()


char line[10];

while (1) {

printf("Enter add(a), delete(d), quit(q): ");

fgets(line, sizeof(line), stdin);

switch (line[0]) {

case 'a':



case 'd':



case 'q':




printf("Error:Bad command %c\n", line[0]);





The ?: Construct

The question mark (?) and colon (:) operators work in a manner similar to that of if/then/else. Unlike if/then/else, the ?: construct can be used inside of an expression. The general form of ?: is:

(expression) ? value1 :value2

For example, the following construct assigns to amount_owed the value of the balance or zero, depending on the amount of the balance:

amount_owed = (balance < 0) ? 0 : balance;

The following macro returns the minimum of its two arguments:

#define min(x,y) ((x) < (y) ? (x) : (y))

The , Operator

The comma (,) operator can be used to group statements. For example:

if (total < 0) {

printf("You owe nothing\n");

total = 0;


can be written as:

if (total < 0)

printf("You owe nothing\n"),total = 0;

In most cases, curly braces ({}) should be used instead of a comma. About the only place the comma operator is useful is in a for statement. The following for loop increments two counters, two and three, by 2 and 3:

for (two = 0, three = 0;

two < 10;

two += 2, three += 3)

printf("%d %d\n", two, three);

volatile Qualifier

The volatile keyword is used to indicate a variable whose value might change at any moment. The keyword is used for variables such as memory-mapped I/O devices or in real-time control applications where variables can be changed by an interrupt routine.

Things like memory-mapped device drivers, interrupt routines, and real-time control are extremely advanced subjects. You will be programming at a level far beyond the scope of this book before you will need to use the volatile keyword.


Answer 21-1: The compiler didn’t see our default line because we misspelled “default” as “defualt.” This mistake was not flagged as an error because “defualt:” is a valid goto label.