-->

Previous | Table of Contents | Next

Page 465

a, the shorthand a += expr is the same as a=a+expr. The expression can be as complex or as simple as required.

NOTE
Most UNIX functions take advantage of the truth values and return 0 for success. This enables a programmer to write code such as

if (function())



        {



        error condition





        }


The return value of a function determines whether the function worked.

Bitwise Operations

Because a variable is just a string of bits, many operations work on those bit patterns. Table 23.6 lists the bit operators.

Table 23.6. Bit operators.


Operator Meaning
& Bitwise AND
| Bitwise OR
~ Negation (one's complement)
<< Bit shift left
>> Bit shift right

A bitwise AND compares the individual bits in place. If both are 1, the value 1 is assigned to the expression. Otherwise, 0 is assigned. For a logical OR, 1 is assigned if either value is a 1. Bit shift operations move the bits a number of positions to the right or left. Mathematically, this is the same as multiplying or dividing by 2, but circumstances exist where the bit shift is preferred.

Bit operations are often used for masking values and for comparisons. A simple way to determine whether a value is odd or even is to perform a bitwise AND with the integer value 1. If it is true, the number is odd.

Statement Controls

With what you've seen so far, you can create a list of statements that are executed only once, after which the program terminates. To control the flow of commands, three types of loops exist in C. The simplest is the while loop. The syntax is


while (expression)

       statement

Page 466

As long as the expression between the parentheses evaluates as nonzero—or true in C—the statement is executed. statement actually can be a list of statements blocked off with curly braces. If the expression evaluates to zero the first time it is reached, the statement is never executed. To force at least one execution of the statement, use a do loop. The syntax for a do loop is


do

        statement

        while (expression);

The third type of control flow is the for loop. This is more complicated. The syntax is


for(expr1;expr2;expr3) statement

When this expression is reached for the first time, expr1 is evaluated, and then expr2 is evaluated. If expr2 is nonzero, statement is executed, followed by expr3. Then expr2 is tested again, followed by the statement and expr3, until expr2 evaluates to zero. Strictly speaking, this is a notational convenience because a while loop can be structured to perform the same actions, as in the following:


expr1;

while (expr2) {

        statement;

        expr3

        }

Loops can be interrupted in three ways. A break statement terminates execution in a loop and exits it. continue terminates the current iteration and retests the loop before possibly reexecuting the statement. For an unconventional exit, you can use goto. goto changes the program's execution to a labeled statement. According to many programmers, goto is poor programming practice, and you should avoid using it.

Statements can also be executed conditionally. Again, there are three different formats for statement execution. The simplest is an if statement. The syntax is


if (expr) statement

If the expression expr evaluates to nonzero, statement is executed. You can expand this with an else, the second type of conditional execution. The syntax for else is


if (expr) statement else statement

If the expression evaluates to zero, the second statement is executed.

NOTE
The second statement in an else condition can be another if statement. This situation might cause the grammar to be indeterminate if the structure

if (expr) if (expr) statement else statement


is not parsed cleanly.

Page 467

As the code is written, the else is considered applicable to the second if. To make it applicable with the first if, surround the second if statement with curly braces, as in the following: if (expr) {if (expr) statement} else statement

The third type of conditional execution is more complicated. The switch statement first evaluates an expression. Then it looks down a series of case statements to find a label that matches the expression's value and executes the statements following the label. A special label default exists if no other conditions are met. If you want only a set of statements executed for each label, you must use the break statement to leave the switch statement.

This covers the simplest building blocks of a C program. You can add more power by using functions and by declaring complex datatypes.

If your program requires different pieces of data to be grouped on a consistent basis, you can group them into structures. Listing 23.3 shows a structure for a California driver's license. Note that it includes integer, character, and character array (string) types.

Listing 23.3. An example of a structure.


struct license {

        char name[128];

        char address[3][128];

        int zipcode;

        int height, weight, month, day, year;

        char license_letter;

        int license_number;

        };



struct license newlicensee;

struct license *user;



Because California driver's license numbers consist of a single character followed by a seven- digit number, the license ID is broken into two components. Similarly, the new licensee's address is broken into three lines, represented by three arrays of 128 characters.

Accessing individual fields of a structure requires two different techniques. To read a member of a locally defined structure, you append a dot to the variable and then the field name, as in the following example:


newlicensee.zipcode=94404;

When using a pointer to a structure, you need -> to point to the member (to reference the individual members):


user->zipcode=94404;

Previous | Table of Contents | Next