TOC
 Operators
 Operator Precedence and Associativity
 Operators That Change Operands
 Alternative Representation of Operators
Operators
Until C99, 50 operators. Since C11, 51 operators, _Alignof
. compound literal [C99] not included in the total.
 6 bitwise operators:
 Unary bitwise NOT
~
.  Binary operators
<<
,>>
,&
,^
,
.
 Unary bitwise NOT
 5 binary operators:
 Bitwise left shift
<<
.  Bitwise right shift
>>
.  Bitwise AND
&
.  Bitwise exclusive or (XOR)
^
.  Bitwise inclusive or (OR)

.
 Bitwise left shift
 3 logical operators:
 Logical AND
&&
.  Logical OR

.  Unary not
!
.
 Logical AND
 11 assignment operators:
 Simple assignment
=
.  Compound assignment, augmented assignment, or combined assignment
*=
,/=
,%=
,+=
,=
,<<=
,>>=
,&=
,^=
,=
.
 Simple assignment
 6 unary operators:
 Address of
&
.  Pointer type
*
.  Unary arithmetic
+
,
,~
,!
.
 Address of
 4 unary arithmetic operators:
 Plus (integer promotion)
+
, result is an arithmetic type.  Minus (additive inverse)

, result is an arithmetic type.  Bitwise NOT
~
, result is integer type.  Logical negation NOT
!
, result is a scalar type ofint
 Plus (integer promotion)
 13 arithmetic operators:
 Unary
+
(+x
) and
(x
).  Additive
+
(x+y
) and
(xy
).  Multiplicative
*
(x*y
),/
(x/y
) and%
(x%y
).  Bitwise
~
(~x
),<<
(x<<y
),>>
(x>>y
),&
(x&y
),^
(x^y
) and
(xy
).
 Unary
 3 multiplicative operators:
 Multiplication
*
.  Division
/
.  Modulo
%
, result is integer type.
 Multiplication
 2 additive operators:
 Addition
+
.  Subtraction

.
 Addition
 6 comparison operators:
 Equality
==
,!=
.  Relational
<
,>
,<=
,>=
.
 Equality
 4 relational operators:
 Less than
<
.  Bigger than
>
.  Less than or equal to
<=
.  Bigger than or equal to
>=
.
 Less than
 2 equality operators:
 Equal to
==
.  Not equal to
!=
.
 Equal to
 5 member access:
 Array subscription
[]
(x[y]
).  Indirection operator, pointer type
*
(*x
).  Address operator
&
(&x
).  Access member of
struct
orunion
.
(x.y
).  Access member of
struct
orunion
using pointer>
(x>y
).
 Array subscription
 2 prefix, increment and decrement operators:
++prefix
andprefix
.  2 postfix, increment and decrement operators:
postfix++
andpostfix
.  7 miscellaneous operators:
 Comma
,
. 
Conditional operator or ternary
? :
,x > y ? 1 : 0;
.  Function call
( )
,x(y,z)
. 
sizeof
, result is size in bytes.  [C11]
_Alignof(
type)
, result is the alignment requirement of the type.  Conversion, cstyle cast:
(
type) x
. 
Compound literal[C99]
(
type){
initializerlist}
, the result is a unnamed object.
 Comma
Notes:
 Compound literal behave as an operator but is not explicit in the standard as part of the group operators, but in this guide is considered an operator.
 Same Symbol Different Operators. The behavior is defined by how it is used:
 Bitwise AND
&
vs Address Of&
.  Multiplication
*
vs Indirection Operator*
(Pointer).  Unary Minus

and Unary Plus+
vs Additive Operators
and+
.
 Bitwise AND
 ๐ฉ Applying comparison operators to Infinite or NotaNumber (NaN) values will cause an exception. NaN does not compare to anything, even between NaN and NaN. To proper compare use
isnan
function from<math.h>
. To compare an Infinite value useisinf
orisfinite
macros.  โ ๏ธ Arithmetic operators applied to floatingpoint types may cause NaN or Infinite value.
Operator Precedence and Associativity
 Associativity: Lefttoright

()
 Function call 
[]
 Array subscripting 
.
 Structure and union member access 
>
 Structure and union member access through pointer 
++
,
 Suffix/postfix increment and decrement 
(type){list}
 Compound literal [C99]

 Associativity: Righttoleft

!
,~
 Logical NOT and bitwise NOT 
++
,
 Prefix increment and decrement 
+
,
 Unary plus and minus 
(type)
 Type cast 
sizeof
 Sizeof 
*
 Indirection (dereference), pointer syntax 
&
 Addressof 
_Alignof
 Alignment requirement [C11]

 Associativity: Lefttoright

*
 Multiplication 
/
 Division 
%
 Modulus

 Associativity: Lefttoright

+
 Addition 

 Subtraction

 Associativity: Lefttoright

<<
 Bitwise left shift 
>>
 Bitwise right shift

 Associativity: Lefttoright

<
 Less than 
<=
 Less than OR equal to 
>
 Greater than 
>=
 Greater than OR equal to

 Associativity: Lefttoright

==
 Equal to 
!=
 NOT equal to

 Associativity: Lefttoright

&
 Bitwise AND

 Associativity: Lefttoright

^
 Bitwise XOR (exclusive or)

 Associativity: Lefttoright


 Bitwise OR (inclusive or)

 Associativity: Lefttoright

&&
 Logical AND

 Associativity: Lefttoright


 Logical OR

 Associativity: RighttoLeft

?:
 Ternary conditional. Test your compiler precedence on this one.

 Associativity: RighttoLeft

=
 Simple assignment 
+=
,=
 Assignment by sum and difference 
*=
,/=
,%=
 Assignment by product, quotient, and remainder 
<<=
,>>=
 Assignment by bitwise left shift and right shift 
&=
,^=
,=
 Assignment by bitwise AND, XOR, and OR

 Associativity: Lefttoright

,
 Comma

Notes:
 Precedence and associativity are independent from order of evaluation.
 C language standard doesn't specify operator precedence.
 When parsing an expression, an operator which is listed on the same row will be bound tighter
(
as if by parentheses)
to its arguments than any operator that is listed on a row further below it. For example, the expression*p++
is parsed as*(p++)
, and not as(*p)++
.  Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction. For example, the expression
a=b=c
is parsed asa=(b=c)
, and not as(a=b)=c
because of righttoleft associativity.
Operators That Change Operands
Most operators does not change the operands values, but a few do:
 Assignment operators change the leftoperand value.
 Increment and decrement operators change the operands value.
This is why some programmers use constant value on the left operand. E.g.: instead of ==
for equality comparison, the programmer mistakable use =
that is the simple assignment, the compiler will not be able to recognize the problem:
if(value = 0){} // ๐ฉ, It is valid expression for the compiler
To better mitigate risks, exchange the position and the compiler will be able to output a proper error if this mistake happen.
if(0 = value){} // Compiler output a proper error on this
Alternative Representation of Operators
<iso646.h>
define 11 macros, that are alternative spellings for some operators. Why <iso646.h>
? European keyboards are not programmingfriendly. Some programmers argue that this make easy to parse by human eyes when heavy use of bitwise operators is done with comparison operators.
Symbol  Alternative spelling  Example 

&& 
and 
x and y 
&= 
and_eq 
x and_eq y 
& 
bitand 
x bitand y 
 
bitor 
x bitor 
~ 
compl 
x compl y 
! 
not 
not x 
!= 
not_eq 
not_eq x 
 
or 
x or y 
= 
or_eq 
x or_eq y 
^ 
xor 
x xor y 
^= 
xor_eq 
x xor_eq y 
References
Discussion (0)