Operators

The tables below describe some of the operators available in the C programming language, and their order of precedence.



Arithmetic Operators
Operator nameExampleDescription
Assignmenta = bAssigns the value of b to a
Unary plus+aSignifies that a has a positive value
Additiona + bReturns the sum of a and b
Prefix increment++aIncrements a and returns its new value
Postfix incrementa++Increments a and returns its original value
Assignment by additiona += bAssigns the value of a plus b to a
Unary minus-aSignifies that a has a negative value
Subtractiona - bReturns the value a minus b
Prefix decrement--aDecrements a and returns its new value
Postfix decrementa--Decrements a and returns its original value
Assignment by subtractiona -= bAssigns the value of a minus b to a
Multiplicationa * bReturns the value of a times b
Assignment by multiplicationa *= bAssigns the value of a times b to a
Divisiona / bReturns the value of a divided by b
Assignment by divisiona /= bAssigns the value of a divided by b to a
Modulusa % bReturns the remainder of a divided by b
Assignment by modulusa %= bAssigns the remainder of a divided by b to a




Comparison Operators
Operator nameExampleDescription
Less thana < bReturns 1 if a is less than b, otherwise 0
Less than or equal toa <= bReturns 1 if a is less than or equal to b, otherwise 0
Greater thana > bReturns 1 if a is greater than b, otherwise 0
Greater than or equal toa >= bReturns 1 if a is greater than or equal to b, otherwise 0
Not equal toa != bReturns 1 if a is not equal to b, otherwise 0
Equal toa == bReturns 1 if a is equal to b, otherwise 0




Logical Operators
Operator nameExampleDescription
Logical negation!aReturns true if a is false, and false if a is true
Logical ANDa && bReturns true if both a and b are true
Logical ORa || bReturns true if either a or b (or both) are true




Bitwise Operators
Operator nameExampleDescription
Bitwise left shifta << bReturns the value of a with its bits shifted b places to the left
Assignment by bitwise left shifta <<= bAssigns the value of a with its bits shifted b places to the left to a
Bitwise right shifta >> bReturns the value of a with its bits shifted b places to the right
Assignment by bitwise right shifta >>= bAssigns the value of a with its bits shifted b places to the right to a
Bitwise one's complement~aReturns the one's complement of a
Bitwise ANDa & bReturns the result of a bitwise AND of a and b
Assignment by bitwise ANDa &= bAssigns the value of a bitwise AND of a and b to a
Bitwise ORa | bReturns the result of a bitwise OR of a and b
Assignment by bitwise ORa |= bAssigns the value of a bitwise OR of a and b to a
Bitwise XORa ^ bReturns the result of a bitwise XOR of a and b
Assignment by bitwise XORa ^= bAssigns the value of a bitwise XOR of a and b to a


Operator Precedence

Operators have a set order of precedence during evaluation. Items within parenthesis (brackets) are evaluated first, and have the highest precedence. The following table shows the order of operator precedence, with the items at the top having the highest precedence. Operators in the same cell have the same precedence.



Operator Precedence
OperatorDescription
++, --Postfix increment and decrement
++, --
+, -
!, ~
Prefix increment and decrement
Unary plus and minus
Logical NOT and bitwise NOT
*, /, %Multiplication, division, and modulus
+, -Addition and subtraction
<<, >>Bitwise left shift and right shift
<, <=
>, >=
Relational "less than" and "less than or equal to"
Relational "greater than" and "greater than or equal to"
==, !=Relational "equal to" and "not equal to"
&Bitwise AND
^Bitwise XOR (exclusive or)
|Bitwise OR (inclusive or)
&&Logical AND
||Logical OR
=
+=, -=
*=, /=, %=
<<=, >>=
&=, ^=, |=
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