6.2 Overview of the C Operators

Variables and constants can be used in conjunction with C operators to create more complex expressions. Table 6-1 presents the set of C operators.

Table 6-1 C Operators

Operator  Example  Description/Meaning 
()   f()   Function call 
[]   a[10]   Array reference 
->   s->a   Structure and union member selection 
.   s.a   Structure and union member selection 
+ [unary]  +a   Value of a 
- [unary]  -a   Negative of a 
* [unary]  *a   Reference to object at address a 
& [unary]  &a   Address of a 
~a   One's complement of a 
++ [prefix]  ++a   The value of a after increment 
++ [postfix]  a++   The value of a before increment 
- - [prefix]  -a   The value of a after decrement 
- - [postfix]  a-   The value of a before decrement 
sizeof   sizeof (t1)   Size in bytes of object with type t1 
sizeof   sizeof e   Size in bytes of object having the type of expression e 
+ [binary]
- [binary]
* [binary]
/ % 
a + b
a - b
a * b
a / b
a % b  
a plus b
a minus b
a times b
a divided by b
Remainder of a/b 
>>
<< 
a >> b
a << b  
a, right-shifted b bits
a, left-shifted b bits 
<
>
<=
>=
==
!=  
a < b
a > b
a <= b
a >= b
a == b
a != b  
1 if a < b; 0 otherwise
1 if a > b; 0 otherwise
1 if a <= b; 0 otherwise
1 if a >= b; 0 otherwise
1 if a equal to b; 0 otherwise
1 if a not equal to b; 0 otherwise 
& [binary]
|
a & b
a | b
a ^ b  
Bitwise AND of a and b
Bitwise OR of a and b
Bitwise XOR (exclusive OR) of a and b 
&&
||
!  
a && b
a || b
!a  
Logical AND of a and b (yields 0 or 1)
Logical OR of a and b (yields 0 or 1)
Logical NOT of a (yields 0 or 1) 
?:  a ? e1 : e2   Expression e1 if a is nonzero;
Expression e2 if a is zero 
=
+=
-=
*=
/=
%=
>>=
<<=
&=
|=
^=
a = b
a += b
a -= b
a *= b
a /= b
a %= b
a >>= b
a <<= b
a &= b
a |= b
a ^= b
e1,e2  
a, after b is assigned to it
a plus b (assigned to a)
a minus b (assigned to a)
a times b (assigned to a)
a divided by b (assigned to a)
Remainder of a/b (assigned to a)
a, right-shifted b bits (assigned to a)
a, left-shifted b bits (assigned to a)
a AND b (assigned to a)
a OR b (assigned to a)
a XOR b (assigned to a)
e2 (e1 evaluated first) 

The C operators fall into the following categories:

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:

x = 7 + 3 * 2;        /* x is assigned 13, not 20   */

The previous statement is equivalent to the following:

x = 7 + ( 3 * 2 );

Using parenthesis in an expression alters the default precedence. For example:

x = (7 + 3) * 2;  /*  (7 + 3) is evaluated first    */

In an unparenthesized expression, operators of higher precedence are evaluated before those of lower precedence. Consider the following expression:

A+B*C

The identifiers B and C are multiplied first because the multiplication operator (*) has higher precedence than the addition operator (+).

Table 6-2 shows the precedence the compiler uses to evaluate the C operators. Operators with the highest precedence appear at the top of the table; those with the lowest appear at the bottom. Operators of equal precedence appear in the same row.

Table 6-2 Precedence of C Operators

Category  Operator  Associativity 
Postfix  () [] -> . ++ - -   Left to right 
Unary  + - ! ~ ++ - - (type) * & sizeof   Right to left 
Multiplicative   * / %  Left to right 
Additive   + -  Left to right 
Shift   << >>  Left to right 
Relational   < <= > >=  Left to right 
Equality   == !=  Left to right 
Bitwise AND  Left to right 
Bitwise XOR  Left to right 
Bitwise OR  Left to right 
Logical AND  &&  Left to right 
Logical OR  ||  Left to right 
Conditional  ?:  Right to left 
Assignment  = += -= *= /= %= >>= <<= &= ^= |=  Right to left 
Comma  Left to right 

Associativity relates to precedence, and resolves any ambiguity over the grouping of operators with the same precedence. In the following statement, the rules of C specify that a * b is evaluated first:

y = a * b / c;

In a more complicated example, associativity rules specify that b ? c : d is evaluated first in the following example:

a ? b ? c : d : e;

The associativity of the conditional operator is right-to-left on the line. The assignment operator also associates right-to-left; for example:

int x = 0 , y = 5, z = 3;
x = y = z;                  /*  x has the value 3, not 5    */

Other operators associate left-to-right; for example, the binary addition, subtraction, multiplication, and division operators all have left-to-right associativity.

Associativity applies to each row of operators in Table 6-2 and is right-to-left for some rows and left-to-right for others. The kind of associativity determines the order in which operators from the same row are evaluated in an unparenthesized expression. Consider the following expression:

A*B%C

This expression is evaluated as follows because the multiplicative operators (*, /, %) are evaluated from left to right:

(A*B)%C

Parentheses can always be used to control precedence and associativity within an expression.


Previous Page | Next Page | Table of Contents | Index