Operators in C Programming

C programming language supports various operators to perform various operations like logical, mathematical, relational, arithmetic, bitwise operators etc. In this tutorial we will learn about various C operators in brief with sample programs.

An operator in C is a symbol used to perform logical and mathematical operations in a C program.

A statement containing operators and variables is called an Expression. C operators connects constants and variables to form expressions.
For Example

In 2 x (length + breadth);
  • x and + are operators
  • 2 is a constant
  • length and breadth are variables
  • "2x(length + breadth)" is an expression, performing one logical task

Arithmetic Operators in C

C Arithmetic operators are used to perform mathematical operations. There are five fundamental arithmetic operators supported by C language, which are addition(+), subtraction(-), multiplication(-), division(/) and modulus(%) of two numbers.

All arithmetic operators compute the result of specific arithmetic operation and returns its result.

Operator Description Syntax Example
+ Adds two numbers a + b 15 + 5 = 20
- Subtracts two numbers a - b 15 - 5 = 10
* Multiplies two numbers a * b 15 * 5 = 75
/ Divides numerator by denominator a / b 15 / 5 = 3
% Returns remainder after an integer division a % b 15 % 5 = 0

C Program to perform Arithmetic Operations on Two Numbers

#include <stdio.h>

int main(){
    /* Variable declation */
    int firstNumber, secondNumber;
    int sum, difference, product, modulo;
    float quotient;
    
    /* Taking input from user and storing it in firstNumber and secondNumber */
    printf("Enter First Number: ");
    scanf("%d", &firstNumber);
    printf("Enter Second Number: ");
    scanf("%d", &secondNumber);
    
    /* Adding two numbers */
    sum = firstNumber + secondNumber;
    /* Subtracting two numbers */
    difference = firstNumber - secondNumber;
    /* Multiplying two numbers*/
    product = firstNumber * secondNumber;
    /* Dividing two numbers by typecasting one operand to float*/
    quotient = (float)firstNumber / secondNumber;
    /* returns remainder of after an integer division */
    modulo = firstNumber % secondNumber;
    
    printf("\nSum = %d", sum);
    printf("\nDifference  = %d", difference);
    printf("\nMultiplication = %d", product);
    printf("\nDivision = %.3f", quotient);
    printf("\nRemainder = %d", modulo);

    return 0;
}
Output
Enter First Number: 25
Enter Second Number: 4

Sum = 29
Difference  = 21
Multiplication = 100
Division = 6.250
Remainder = 1

Assignment Operators in C Programming

Assignment Operators in C is used to assign a value to a variable. "=" is called simple assignment operator of C, it assigns values from right side operands(R value) to left side operand (L value).

The general syntax of assignment operator is:

variable_name = expression;
For Example
  • value = 1234;
  • value = 4/2;

C also supports compound assignment operators or shorthand assignment operators. These operators are combination of arithmetic and assignment operators.
They first perform arithematic operation between left and right operand and then assign the result to left operand. It first add 5 to A, and then assign result to A.

For Example

  • A+=5; is equivalent to A = A+5;
    It first add 5 to A, and then assign result to A.

Below is the list of Shorthand Assignment Operators Supported in C.

Operator Example Equivalent Expression Description
+= A += B; A = A+B; It Adds A and B, then assign result to A
-= A -= B; A = A-B; It subtract B from A, then assign result to A
/= A /= B; A = A/B; It divides A by B, then assign result to A
*= A *= B; A = A*B; It multiply A and B, then assign result to A
%= A %= B; A = A%B; It takes modulus of A by B, then assign result A
&= A &= B; A = A&B; It does bitwise AND between A and B, then assign result to A
|= A |= B; A = A|B; It does bitwise OR between A and B, then assign result to A
^= A ^= B; A = A^B; It does bitwise XOR between A and B, then assign result to A
>>= A >>= B; A = A>>B; It does bitwise right shift, then assign result to A
<<= A <<= B; A = A< It does bitwise left shift, then assign result to A

C Program to show use of Shorthand Assignment Operators

#include<stdio.h>

int main() {
   int left_Op;

   left_Op = 10;
   /* A+=B; is equivalent to A = A+B;*/
   left_Op += 5;
   printf("Result = %d\n", left_Op);

   left_Op = 10;
   /* A-=B; is equivalent to A = A-B;*/
   left_Op -= 5;
   printf("Result = %d\n", left_Op);

   left_Op = 10;
   /* A/=B; is equivalent to A = A/B;*/
   left_Op /= 5;
   printf("Result = %d\n", left_Op);

   left_Op = 10;
   /* A%=B; is equivalent to A = A%B;*/
   left_Op %= 5;
   printf("Result = %d", left_Op);

   return 0;
}
Output
Result = 15
Result = 5
Result = 2
Result = 0

Relational Operators in C

Relational Operators in C programming are used to compare two values. It specifies the relation between two values like equal, greater than, less than etc. Relational operators always returns boolean value (zero for false and non-zero value for true).
For Example
(A > B) : It checks whether A is greater than B or not. It will return none-zero(true) if A is greater than B otherwise zero(false).

Below is the list of Relational Operators Supported in C

Relational Operator Example Description
> A > B Checks if A is greater than B
< A < B Checks if A is less than B
>= A >= B Checks if A is greater than or equal to B
<= A <= B Checks if A is less than or equal to B
== A == B Checks if A is equal to B
!= A != B Checks if A is not equal to B

C Program to compare two numbers using Relational Operators

#include<stdio.h>

int main(){

    int A = 10, B = 5;

    if(A>B){
        printf("A is greater than B\n");
    } else {
        printf("A is less than B\n");
    }
    
    if(A == B){
        printf("A is equal to B\n");
    } else {
        printf("A is not equal to B\n");
    }
    
    return(0);
}
Output
A is greater than B
A is not equal to B

Logical Operators in C

Logical Operators in C programming language combines multiple boolean expressions. If we want to check more than one condition, then we need logical Operators.

Logical Operators always produces boolean results, either TRUE(non-zero value) or FALSE(zero value).
There are 3 logical operators in C language AND(&&), OR(||) and NOT(!)

For Example
     "If Humidity is less than 10% and Temperature is more than 30 degree celsius"
We can implement above mentioned condition using logical operator as follows:
if((Humidity < 10) && (Temperature > 30))

Description of Logical Operators in C

  • AND : Returns true, If both operands are true otherwise false.
  • OR : Returns false If both operands are false, otherwise true.
  • NOT : Returns true if operand is false and Returns false if operand is true.
Here is the truth table of Logical Operators
A B A && B A || B !A
TRUE FALSE FALSE TRUE FALSE
TRUE TRUE TRUE TRUE FALSE
FALSE TRUE FALSE TRUE TRUE
FALSE FALSE FALSE FALSE TRUE

C Program to show use of Logical Operators

#include<stdio.h>

int main(){

    int A = 10, B = 5;
    /*Using AND Logical Operator to combime 
      two relational expressions */
    if((A>B) && (B<7)){
        printf("Logical Expression returned TRUE\n");
    } else {
        printf("Logical Expression returned FALSE\n");
    }
    
    return(0);
}
Output
Logical Expression returned TRUE

Bitwise Operators in C

In this tutorial we will learn about bitwise operators in C. C supports OR, AND, NOT, XOR, Right shift and Left shift bitwise operators.

C is a middle level language, it support many operations which can be performed in assembly language like operations on bits. Bitwise operators performs bit-by-bit operations on operands.

There are six bitwise operators supported in C programming language.

Bitwise Operator Syntax Description
| A | B Bitwise OR Operator
& A & B Bitwise AND Operator
~ ~A NOT Operator(One's Complement)
^ A ^ B Bitwise Exclusive OR Operator
>> A >> B Right Shift Operator
<< A << B Left Shift Operator

Bitwise Operators can only be applied on char and integer operands. We cannot use bitwise operators with float, double, long double, void and other user define complex data types.
Here is the truth table of &, |, ^ and ~ bitwise operators.

A B A | B A & B A ^ B ~A
0 0 0 0 0 1
0 1 1 0 1 1
1 1 1 1 0 0
1 0 1 0 1 0

C Program to show the use of AND, OR, XOR, NOT and Right/Left Shift Bitwise Operators

#include <stdio.h>

int main(){
   /*
      A = 29 = 00011101(in binary)
      B = 42 = 00101010(in binary)
   */
   int A = 29, B = 42;

   /* Bitwise OR */    
   printf("A | B = %d\n", A | B);
   /* Bitwise AND */
   printf("A & B = %d\n", A & B);
   /* Bitwise XOR */
   printf("A ^ B = %d\n", A ^ B);
   /* Bitwise NOT */
   printf("~A = %d\n", ~A);
   /* Bitwise Right Shift Operator */   
   printf("A >> 3 = %d\n", A >> 3);
   /* Bitwise Left Shift Operator */
   printf("A << 3 = %d\n", A << 3);
   
   return 0;
}
Output
A | B = 63
A & B = 8
A ^ B = 55
~A = -30
A >> 3 = 3
A << 3 = 232

Conditional Operators in C

Conditional Operator in C is a powerful Operator which can be used to implement if-then-else type of logic. This operator is also known as ternary operator as it takes three expressions in following form.

Conditional_Expression ? Expression_One : Expression_Two;
Ternary Operator will execute Expression_One if Conditional_Expression is true, otherwise it execute Expression_Two.
Ternary Operator is similar to if-else decision block as it evaluates only one code block depending on the result of Conditional_Expression
For Example
int X = 25;
int Y = (X > 20 ? 1 : 2);
As X > 20, So after above statement Y's value becomes 1.

C Program to find larger number using Conditional Operator

Below program takes two numbers as input from user and stores them in integer variable 'A' and 'B'. Then using ternary operator it prints whether A >= B or A < B.

#include<stdio.h>

int main(){

    int A, B;
    
    printf("Enter two numbers\n");
    scanf("%d %d", &A, &B);
    
    (A>=B) ? printf("%d is >= %d\n",A,B) : printf("%d is < %d\n",A,B);
    
    return(0);
}
Output
5 4
5 is >= 4

Special Operators in C

Below are some special operators supported by C language.

Operators Description Example
* It represents a pointer to a variable. int *ptr; ptr is a pointer to variable of data type int.
& Returns address of the variable. &Val will give address of Val variable.
Sizeof() Returns the size of a variable or data type. It is not a function. Sizeof(int); Returns the size of integer data type.
Address of Operator(&)

The & is a unary operator in C which returns the memory address of the passed operand. This is also known as "address of" operator.

A pointer contains the memory address of some object.

Value of Operator(*)

The * is a unary operator which returns the value of object pointer by a pointer variable. It is known as value of operator.

It is also used for declaring pointer variable.

For Example
    int A = 100;
    int *ptr = &A;

In the first statement, we first declare an integer variable and initialize it with value 100. In second statement, we are declaring a pointer to a variable of type int and initializing it with address of A.

Sizeof Operator

The sizeof is a compile time operator not a standard library function. The sizeof is a unary operator which returns the size of passed variable or data type in bytes.

As we know, that size of basic data types in C is system dependent, So we can use sizeof operator to dynamically determine the size of variable at run time.


C program to show use of Sizeof, & and * Operator

#include<stdio.h>

int main()
{
    int A = 10;
    /* Use of & and * Operator */
    int *ptr = &A;
    printf("Value of A is %d\n", *ptr);
    
    /* Use of Sizeof Operator to dynamically determine
       the size of data types and variables */
    printf("Size of variable A is %d\n", sizeof(A));
    printf("Size of an Integer variable is %d\n", sizeof(int));
    printf("Size of long int variable is %d\n", sizeof(long int));
    printf("Size of char variable is %d\n", sizeof(char));
    printf("Size of float variable is %d\n", sizeof(float));
    
    return(0);
}
Output
Value of A is 10
Size of variable A is 4
Size of an Integer variable is 4
Size of long int variable is 4
Size of char variable is 1
Size of float variable is 4

Operator Precedence

An expression in C may contains more than one variables and operators. Not every operator in C is of same precedence, some operators are of higher priority than others; for example increment operator(++) is of higher precedence than addition operator(+). Operator precedence defines the sequence of evaluation for operators of an expression, to resolve and ambiguity.

For Example
    X = 1 + 2 * 3;
Without any Operator precedence, above expression can be evaluated in two different ways producing two different values for X.
Performing addition before multiplication
      X = 3 * 3 = 9

Performing multiplication before addition
      X = 1 + 6 = 7

To resolve this confusion, we assign different precedence to different operator. As the precedence of multiplication is more than addition, Correct value of X is 7.


The associativity specifies the operators direction to be evaluated, it may be left to right or right to left.

Below table lists the operators in decreasing order of their precedence.
Operator Associativity
() [] -> . Left to right
! ~ -- ++ (type)* & sizeof() Right to left
/ * % Left to right
- + Left to right
<< >> Left to right
< <= > >= Left to right
== != Left to right
& Left to right
^ Left to right
| Left to right
&& Left to right
|| Left to right
?: Right to left
= += -= *= /= %=>>= <<= &= ^= |= Right to left
, Left to right

Points to Remember

  • If two operators of same priority appear at a same level, more priority is given to the operator appearing first.
    For Example :
    X = A*B + C/D;
    In above expression, * and / both have the same priority but * will be evaluated first as it appears first.
  • In any expression containing parentheses, innermost parentheses is given more priority then the outer one and so on.
    For Example :
    X = 2 * (3 + 4);
    X = 2 * 7;      /* + gets evaluated first because of parentheses */
    X = 14;