Micron Document
The C Programming Language, Ansi C


← Prev · 63/277 · Next →

It should be noted that the conditional expression is indeed an expression,
and it can be used wherever any other expression can be. If

expr

2

and

expr

3

are of different
types, the type of the result is determined by the conversion rules discussed
earlier in this chapter. For example, if

f

is a

float

and

n

an

int

, then the expression

(n > 0) ? f : n

is of type

float

regardless of whether

n

is positive.

Parentheses are not necessary around the first expression of a conditional expression, since the precedence of ?: is very low, just above assignment. They are advisable anyway, however, since they make the condition part of the expression easier to see.

The conditional expression often leads to succinct code. For example, this loop prints n elements of an array, 10 per line, with each column separated by one blank, and with each line (including the last) terminated by a newline.

for (i = 0; i < n; i++)
printf("%6d%c", a[i], (i%10==9 || i==n-1) ? 'n' : ' ');

A newline is printed after every tenth element, and after the

n

th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.th.
All other elements are followed by one blank. This might look tricky, but it's
more compact than the equivalent

if-else

. Another good example is

printf("You have %d items%s.n", n, n==1 ? "" : "s");

Exercise 2-10.

Rewrite the function

lower

, which
converts upper case letters to lower case, with a conditional expression
instead of

if-else

.

Table 2.1 summarizes the rules for precedence and associativity of all
operators, including those that we have not yet discussed. Operators on the
same line have the same precedence; rows are in order of decreasing
precedence, so, for example,

*

,

/

, and

%

all have
the same precedence, which is higher than that of binary

+

and


. The operator''

()

refers to function call. The
operators

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

and

.

are used to access members of
structures; they will be covered in

Chapter 6

,
along with

sizeof

(size of an object).

Chapter 5

discusses

*