Operators act upon objects of a certain type or types and specify what is to be done to them. Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs.
Each of the data types has its own set of operators that determine which operations may be performed on them.
The following operations are supported on integers:
+
, subtraction -
,
multiplication *
, division /
.
>
, less-than <
, equality
==
, greater-than-equal >=
, less-than-equal <=
.
|
, bitwise-AND &
,
bitwise-exclusive-OR ^
, bitwise-NOT ~
.
||
, logical-AND &&
,
logical-NOT !
.
When a C statement uses a boolean value (for example, if
), it takes
the integer zero as meaning false, and any integer other than zero as
meaning true. The boolean operators return zero for false and one
for true.
Boolean operators &&
and ||
may stop executing as soon
as the truth of the final expression is determined. For example, in
the expression a && b
, if a
is false, then b
does
not need to be evaluated because the result must be false. The
&&
operator therefore may not evaluate b
.
A subset of the operations implemented for integers are implemented for
long integers: arithmetic addition +
, subtraction -
, and
multiplication *
, and the integer comparison operations. Bitwise
and boolean operations and division are not supported.
IC uses a package of public-domain floating point routines distributed by Motorola. This package includes arithmetic, trigonometric, and logarithmic functions.
The following operations are supported on floating point numbers:
+
, subtraction -
,
multiplication *
, division /
.
>
, less-than <
, equality
==
, greater-than-equal >=
, less-than-equal <=
.
Characters are only allowed in character arrays. When a cell of the array is referenced, it is automatically coerced into a integer representation for manipulation by the integer operations. When a value is stored into a character array, it is coerced from a standard 16-bit integer into an 8-bit character (by truncating the upper eight bits).
The basic assignment operator is =
. The following statement adds 2 to
the value of a
.
a = a + 2;
The abbreviated form
a += 2;
could also be used to perform the same operation.
All of the following binary operators can be used in this fashion:
+ - * / % << >> & ^ |
The increment operator "++
" increments the named variable.
For example, the statement "a++
" is equivalent to "a=
a+1
" or "a+= 1
".
A statement that uses an increment operator has a value. For example, the statement
a= 3; printf("a=%d a+1=%d\n", a, ++a);
will display the text "a=3 a+1=4
."
If the increment operator comes after the named variable, then the value of the statement is calculated after the increment occurs. So the statement
a= 3; printf("a=%d a+1=%d\n", a, a++);
would display "a=3 a+1=3
" but would finish with a
set to 4.
The decrement operator "--
" is used in the same fashion as the
increment operator.
&
*
[expr]
.
->
f.i
and
(&f)->i
are equivalent.
The following table summarizes the rules for precedence and associativity for the C operators. Operators listed earlier in the table have higher precedence; operators on the same line of the table have equal precedence.
Operator | Associativity | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
() [] | left to right | ||||||||||||||||||||||||||||
! ~ ++ -- - (type) | right to left | ||||||||||||||||||||||||||||
* / % | left to right | ||||||||||||||||||||||||||||
+ - | left to right | ||||||||||||||||||||||||||||
<< >> | left to right | ||||||||||||||||||||||||||||
< <= > >= | left to right | ||||||||||||||||||||||||||||
== != | left to right | ||||||||||||||||||||||||||||
& | left to right | ||||||||||||||||||||||||||||
^ | left to right | ||||||||||||||||||||||||||||
| | left to right | ||||||||||||||||||||||||||||
&& | left to right | ||||||||||||||||||||||||||||
|| | right to left | ||||||||||||||||||||||||||||
= += -= etc. | right to left | ||||||||||||||||||||||||||||
, | left to right |