Primary Operators

Operators Description
( ) Parentheses Used for Expression Grouping

Use parentheses to explicitly force the order of expression evaluation. The following expression does not contain any parentheses used for grouping operands and operators. The parentheses surrounding weightzipcode are used to form a function call. Note how the compiler groups the operands and operators in the expression according to the rules for operator precedence and associativity:


-discount * item + handling(weight, zipcode) < .10 * item
³       ³      ³   ³                       ³   ³        ³
'---.---'      ³   '-----------.-----------'   '----.---'
    '----.-----'               ³                    ³
         '----------.----------'                    ³
                    '-------------------------------'

The following expression is similar to the previous expression, but it contains parentheses that change how the operands and operators are grouped:


(-discount * (item + handling(weight, zipcode) ) ) < (.10 * item)
 ³       ³    ³      ³                       ³       ³          ³
 '---.---'    ³      '----------.------------'       '-----.----'
     ³        '--------.--------'                          ³
     '-------.---------'                                   ³
             '---------------------------------------------'

In an expression that contains both associative and commutative operators, you can use parentheses to specify the grouping of operands with operators. The parentheses in the following expression guarantee the order of grouping operands with the operators:

x = f + (g + h);
[ ] Array Subscripts

A primary expression followed by an expression in [ ] (square brackets) specifies an element of an array. The expression within the square brackets is referred to as a subscript.

The primary expression must have a pointer type, and the subscript must have integral type. The result of an array subscript is an lvalue.

The first element of each array has the subscript 0. The expression contract[35] refers to the 36th element in the array contract.

In a multidimensional array, you can reference each element (in the order of increasing storage locations) by incrementing the rightmost subscript most frequently.

For example, the following statement gives the value 100 to each element in the array code[4][3][6]:

for (first = 0; first <= 3; ++first)
   for (second = 0; second <= 2; ++second)
      for (third = 0; third <= 5; ++third)
         code[first][second][third] = 100;
.
->
Structure and Union Member Specification

Two primary operators let you specify structure and union members. The dot (a period) and arrow (formed by a minus and a greater than symbol) operators are always preceded by a primary expression and followed by an identifier.

When you use the dot operator, the primary expression must be an instance of a type of structure or union, and the identifier must name a member of that structure or union. The result is the value associated with the named structure or union member. The result is an lvalue if the first expression is an lvalue.

Some sample dot expressions:

roster[num].name
roster[num].name[1]

When you use the arrow operator, the primary expression must be a pointer to a structure or a union, and the identifier must name a member of the structure or union. The result is the value of the named structure or union member to which the pointer expression refers. In the following example, name is an int:

roster -> name


Operator Precedence and Associativity
Expressions and Operators
Types of Expressions


Operator Precedence and Associativity Table
Unary Operators
Binary Operators
Conditional Operator
Assignment Operators
Comma Operator
Array Type
struct (Stuctures) Type
union (Unions) Type