An operator is a token that specifies an operation on at least one operand, and yields some result (a value, designator, side effect, or some combination). Operands are expressions or constants (a form of expression). Operators with one operand are unary operators, and operators with two operands are binary operators. For example:
x = -b; /* Unary minus operator */ y = a - c; /* Binary minus operator */
Operators with three operands are called ternary operators.
All operators are ranked by precedence, a ranking system determining which operators are evaluated before others in a statement. See Chapter 6 for information on what each operator does and for the rules of operator precedence.
Some operators in C are composed of more than one character, while others are single characters. The single-character operators in C are:
! % ^ & * - + = ~ | . < > / ? : , [ ] ( ) #
The multiple-character operators in C are:
++ -- -> << >> <= >= == != *= /= %= += -= <<= >>= &= ^= |= ## && ||
The #
and ##
operators can only be used
in preprocessor macro definitions. See Chapter 8 for more information on predefined macros and
preprocessor directives.
The sizeof
operator determines the size of a data type.
See Chapter 6 for more information on the
sizeof
operator.
The old form for compound assignment operators (=+
, =-
, =*
, =/
,
=%
, =<<
, =>>
,
=&
, =^
, and =|
) is not
supported by the ANSI C standard. Use of these operators in a
program is unsupported, and will produce unpredictable results.
For example:
x =-3;
This construction means x
is assigned the value
-3
, not x
is assigned the value
x - 3
.
The error-checking compiler option provides a warning message when the old form of compound assignment operators is encountered.