GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

Operators in C

Operators determine the basis of how strong a programming language. The operators are a powerful tool which lets us perform many operations. The operators are the one which act on the operands to give the answer. 

There are three types of operators in C based on the number of operands it is acting on.

  1. Unary operators (act on single operand)
  2. Binary operators (act on two operands)
  3. Ternary operators (act on three operands)

Say for example, we want to calculate 2+3*4, the answer is 14. We apply multiplication first over addition, due to BODMOS rule of mathematics. Similarly in C we have 15 different types of operators. To know which operator to apply first we have precedence chart for the operators. 

If we have expression like 2+3-4, we can see from precedence chart that both + and – have same precedence of 4. Which to apply first is addition or subtraction. This is decided by associativity. Whenever we have two or more operators with same precedence, we resolve it with associativity. 

Associativity Left to Right means, evaluate first what is coming in the left of given expression and Right to Left is vice-versa. (More about it we will see in expression evaluation).

Operator precedence chart

Precedence # Category Operators Associativity
1
Brackets
( )
Left to Right
2
Unary
+ , - , ++ , -- , &, * , ~, ! , sizeof
Right to Left
3
Multiplicative
*, /, %
Left to Right
4
Additive
+, -
Left to Right
5
Shift
>> , <<
Left to Right
6
Relational
>, >=, <, <=
Left to Right
7
Equality
==, !=
Left to Right
8
Bitwise AND
&
Left to Right
9
Bitwise XOR
^
Left to Right
10
Bitwise OR
|
Left to Right
11
Logical AND
&&
Left to Right
12
Logical OR
||
Left to Right
13
Conditional
?:
Right to Left
14
Assignment
=, +=, -=, *=, /=, %=,>>=, <<=, &=, ^=, |=
Right to Left
15
Comma
,
Left to Right

In the next sections we will discuss these operators one by one in detail with examples.

Scroll to Top