Operators in C programming

In this tutorial, we will learn about operators in C and  learn what are there function as well how to use them.

operators in C programming

Operators in C introduce some mathematical or logical manipulation. for eg .  +  add two numbers.

x + y

In above example,  x and y are operands and + is an operator

Operators which require two operands are called binary operators and which takes single operand are called unary operators.

Operators in C programming

1: Arithmetic operator
2: Relational operator
3: Logical operator
4: Assignment operator
5: Increment/Decrement operator
6: Conditional operator
7: Bitwise operator
8: Special operator

1. Arithmetic operator

Mathematical operators like addition, subtraction, multiplication or division.

Arithmetic operatorMeaning
+Addition or unary plus
Subtraction or unary minus
%Modulo division

2. Relational operator

There are six relational operators:

Relational operatorMeaning
<is less than
<=is less than or equal to
>is greater than
>=is greater than or equal to
==is equal to
!=is not equal to

Relational expressions evaluate to 1 if they are TRUE and 0 if they are FALSE

a > b

In above expression, if a > b it will  1 if a is greater than b else it will return 0

A very common programming error is to mistakenly type = (assignment) for == (equality).

3. Logical operator

When there is a condition to test more than one condition then logical operators come to action.

Logical operatorMeaning
&&logical AND
||logical OR
!logical NOT

Example: logical AND(&&) operator

If we are to make sure all the condition true.

if (age > 11 && age <20)

In above example, for the people to be teens there age must be greater than 11 and less than 20.

Example : logical OR(||) operator

if we are to make sure  either or both of two conditions are true.

if (mark < 40 || attendance < 15)
      printf("Student is not qualified for exam");

In this case, if any of the conditions is true then if statement is also true and the message Student is not qualified for exam is printed.


a=1, b=2, c=3, d=3

(a < b && b < c && c < d) /* FALSE */
(a < b && b < c && c <= d) /* TRUE */
((a < b && b < c) || c < d) /* TRUE */

In expression ((a < b && b < c) || c < d). The order of evaluation of && and || is left-to-right, and evaluation stops as soon as the truth or false of the result is known leaving the remaining expressions.

i.e if  (a < b && b < c) return true expression becomes (T || c < d).  The outpcomes of c < d will not change the result as T || T/F always return  T

4. Assignment operator

Operator used to assign value to variable or constant. 

=,  +=, -=, *=, /=, %=    are the assignment operator

a = 1

In above example,  the left-hand-side variable is assigned the value from right-hand-side. 

a = 1  // value of a is 1
a += 2  // value of a is 3

In above example, the expression a += 2 can be break down to a = a + 2 .


5.  Increment/Decrement operator

The ++ increment and — decrements are two operator that change the value by 1.  ++  add 1 to the operand and — subtract 1 from the operand.

OperatorSample expressionExplanation
++++xx is increased by 1, then use the value of x
++x++Use the current value of x and then increment x by 1
– –– -xx is decreased by 1, then use the value of x
– –x- –Use the current value of x and then decrement x by 1
m = 5;
++ m; // m = 6
-- m; // m = 7
m ++; // m = 6
m --; // m = 5

Here, the operators are used as prefix and postfix. In ++m and –m operators are used as prefix. First the value of m is incremented by 1 and m is returned.

In m++  and m– operators are used as postfix. First the value of m is returned and only then value of m is incresed.

6. Conditional operator

Ternary operator is the Conditional operator. It takes three operands.

val = expression ? a: b ;

Here, if experssion is true then value of a is assigned to val else b is assigned.

7. Bitwise operator

Bitwise operators are used to manipulate data at a bit level. They are used for testing or shifting the bit.

Bitwise operatorMeaning
&bitwise AND
|bitwise OR
^bitwise exclusive OR
<<shift left
>>shift right

8. Special operator

C provides some other special operators like comma operator and sizeof operator.

Comma operator 

It is used to combine more than one expression. A comma linked list of expressions are evaluated left to right.

sizeof operator in C

sizeof operator is an operator which when used with operand returns the number of bytes occupied by the operand.

For example

x = sizeof( a );

Here, the size occupied by variable a will be assigned to x.