Skip to content

Operators#

The C programming language has almost 50 different operators.

Operator Precedence in C
Category Operators Priority
Primary Operators () Highest
Unary Operators - + ! ++ --
Multiplicative Operators * / %
Additive Operators + -
Shift Operators << >>
Relational Operators < <= > >=
Equality Operators == !=
Bitwise AND &
Bitwise XOR ^
Bitwise OR |
Logical AND &&
Logical OR ||
Assignment Operators = Lowest

Arithmetic Operators#

The C programming language has the following operators for doing arithmetic:

Arithmetic Operators in C
Operator Name Syntax Associativity Description
+ addition Operand1 + Operand2 Left-to-Right Adds the value of Operand 1 to the value of Operand 2.
- subtraction Operand1 - Operand2 Left-to-Right Subtracts the value of Operand 2 from the value of Operand 1.
* multiplication Operand1 * Operand2 Left-to-Right Multiplies the value of Operand 1 by the value of Operand 2.
/ division Operand1 / Operand2 Left-to-Right Divides the value of Operand 1 by the value of Operand 2.
% modulus Operand1 % Operand2 Left-to-Right Returns the remainder of the division of Operand 1 by Operand 2.

Logical Operators#

Logical Operators in C
Operator Name Syntax Associativity Description
&& logical AND Exp1 && Exp2 Left-to-Right True if both operands are non-zero.
|| logical OR Exp1 || Exp2 Left-to-Right True if at least one operand is non-zero.
! logical NOT !Expression Right-to-Left Reverses the logical state of the operand.

Relational Operators#

The C programming language has the following operators for comparisons:

Relational Operators in C
Operator Name Syntax Associativity Description
== equal to Exp1 == Exp2 Left-to-Right Evaluates to 1 if values are equal.
!= not equal to Exp1 != Exp2 Left-to-Right Evaluates to 1 if values are not equal.
<= less or equal Exp1 <= Exp2 Left-to-Right Evaluates to 1 if Exp1 is less than or equal to Exp2.
>= greater or equal Exp1 >= Exp2 Left-to-Right Evaluates to 1 if Exp1 is greater than or equal to Exp2.
< less than Exp1 < Exp2 Left-to-Right Evaluates to 1 if Exp1 is less than Exp2.
> greater than Exp1 > Exp2 Left-to-Right Evaluates to 1 if Exp1 is greater than Exp2.

Bitwise Operators#

Bitwise Operators in C
Operator Name Syntax Associativity Description
& bitwise AND Op1 & Op2 Left-to-Right Copies a bit to the result if it exists in both operands.
| bitwise OR Op1 | Op2 Left-to-Right Copies a bit to the result if it exists in either operand.
^ bitwise XOR Op1 ^ Op2 Left-to-Right Copies a bit to the result if set in one operand but not both.
~ ones complement ~Operand Right-to-Left Inverts the bits of the operand.
<< left shift Op << n Left-to-Right Shifts bits to the left by n positions.
>> right shift Op >> n Left-to-Right Shifts bits to the right by n positions.

Assignment Operators#

Assignment Operators in C
Operator Name Syntax Associativity Description
= simple assignment Var = Exp Right-to-Left Assigns the value of the right operand to the left operand.
+= add and assign Var += Exp Right-to-Left Adds right operand to left and assigns result to left.
-= subtract and assign Var -= Exp Right-to-Left Subtracts right operand from left and assigns result to left.
*= multiply and assign Var *= Exp Right-to-Left Multiplies left by right and assigns result to left.
/= divide and assign Var /= Exp Right-to-Left Divides left by right and assigns result to left.