Programming in C

(Barry) #1

378 Chapter 17 Miscellaneous and Advanced Features


The type of application illustrated might be practical for storage of a symbol table, for
example, which might contain the name of each symbol, its type, and its value (and per-
haps other information about the symbol as well).

The Comma Operator


At first glance, you might not realize that a comma can be used in expressions as an
operator.The comma operator is at the bottom of the precedence totem pole, so to
speak. In Chapter 5, “Program Looping,” you learned that inside a forstatement you
could include more than one expression in any of the fields by separating each expres-
sion with a comma. For example, the forstatement that begins
for ( i = 0, j = 100; i != 10; ++i, j -= 10 )

initializes the value of ito 0 andjto 100 before the loop begins, and increments the
val ue of iandsubtracts 10 from the value of jeach time after the body of the loop is
executed.
The comma operator can be used to separate multiple expressions anywhere that a
valid C expression can be used.The expressions are evaluated from left to right. So, in
the statement
while ( i < 100 )
sum += data[i], ++i;
the value of data[i]is added into sumand then iis incremented. Note that you don’t
need braces here because just one statement follows the whilestatement. (It consists of
two expressions separated by the comma operator.)
Because all operators in C produce a value, the value of the comma operator is that
of the rightmost expression.
Note that a comma, used to separate arguments in a function call, or variable names
in a list of declarations, for example, is a separate syntactic entity and is notan example of
the use of the comma operator.

Type Qualifiers


The following qualifiers can be used in front of variables to give the compiler more
information about the intended use of the variable and, in some cases, to help it generate
better code.

The registerQualifier


If a function uses a particular variable heavily, you can request that access to the variable
be made as fast as possible by the compiler.Typically, this means requesting that it be
stored in one of the machine’s registers when the function is executed.This is done by
prefixing the declaration of the variable by the keyword register, as follows:
Free download pdf