Table of Contents
< All Topics
Print

9. Operators in C

C operators are symbols that are used to perform mathematical or logical manipulations. C programming language is rich with built-in operators.

C is a widely used programming language with many built-in operators for carrying out various actions and tasks as per program needs. An operator in C plays a role in manipulating data and performing operations on variables and values.

These symbols are operators, each representing a particular operation to be carried out on operands. They are used in conditional, mathematical, and logical expressions.

There is a wide range of operators in the C language; each belongs to a different category according to its functionalities.

Operators in C language take part in a program for manipulating data and variables and it also form a part of the mathematical or logical expressions.

Types of Operators in C

  • Arithmetic Operators
  • Increment and Decrement Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Conditional Operator
  • Bitwise Operators
  • Special Operators

Arithmetic Operators

Arithmetic operations are used for mathematical or arithmetic calculations, such as division (/), addition (+), subtraction (-), multiplication (*), etc., on operands. It performs operations on constants and variables, i.e., numerical values.

OPERATIONOPERATORSYNTAXCOMMENTRESULT
Multiply*a * bresult = a * b27
Divide/a / bresult = a / b3
Addition+a + bresult = a + b12
Subtractiona – bresult = a – b6
Modulus%a % bresult = a % b0
//Example:
#include <stdio.h> 

int main() 
{ int i=3,j=7,k; 
k=i+j; 
printf("sum of two numbers is %d\n", k);
return 0; 
}

Output

Increment and Decrement Operators

  • Increment and Decrement Operators are useful operators generally used to minimize the calculation, for example.
    • ++x & x++ this means x=x+1
    • or
    • –x & x−− this means x=x-1.
  • But there is difference between ++ or −− written before or after the operand.
  • Applying the pre-increment first add one to the operand and then the result is assigned to the variable on left.
  • whereas post-increment first assigns the value to the variable on the left and then increment the operand.
OperatorDescription
++Increment
−−Decrement

Increment and decrement operators are exclusively used with variables and cannot be applied to constants or expressions.

//let us say:
int x = 5;

x++; // Now x is 6

x--;// Now x is 5

// a valid and invald use of pre/post inc/decrement operator
int x = 1, y = 1;

++x;     // valid

++5;      // invalid - increment operator operating on a constant value

++(x+y);  // invalid - increment operating on an expression

Example

#include<stdio.h>
int main()
{   
  int x=5, y=7;
 printf(“ the pre increment operation %d”, ++x);  // first increment , then print
 printf(“ the pre increment operation %d”, ++y);  // first increment , then print

 printf(“ the post increment operation %d”, x++); // first print, then increment 
 printf(“ the post increment operation %d”, y++); // first print, then increment 
 printf(“ the post increment operation %d”, x++); // first print, then increment 
   
 return 0;
}

Output

Relational Operators

Relational operators in C compare the values of two operands in a program to determine their relationship. If the relationship is true, it returns 1; otherwise, it returns 0. These operators are commonly used for decision-making and loop operations.

For example, checking if one operand is greater or equal to another. Relational operators include ==, >= , <= .

OPERATORMEANINGEXAMPLE
<LESS THAN3 < 5 GIVES 1
>GREATER THAN7 > 9 GIVES 0
>=LESS THAN OR EQUAL TO100 >= 100 GIVES 1
<=GREATER THAN EQUAL TO50 >=100 GIVES 0
==RETURNS 1 IF BOTH OPERANDS ARE EQUAL, 0 OTHERWISE(4 == 9) is not true.
!=RETURNS 1 IF OPERANDS DO NOT HAVE THE SAME VALUE, 0 OTHERWISE(4 != 9) is true.

Example: Program to check role of relational operators

// sample code to check role of relational operators
// By milndbhatt
#include<stdio.h>

int main()
{
    int a = 10, b = 20;

    printf("a = %d\n", a);
    printf("b = %d\n\n", b);

    // Check a is greater than b.
    printf("a > b : %d\n", a > b);

    // Check a is greater than or equal to b.
    printf("a >= b : %d\n", a >= b);

    // Check a is smaller than b.
    printf("a < b : %d\n", a < b);

    // Check a is smaller than or equal to b.
    printf("a <= b : %d\n", a <= b);

    // Check a is equal to b.
    printf("a == b : %d\n", a == b);

    // Check a is not equal to b.
    printf("a != b : %d\n", a != b);

    
    return 0;
}

Output

Next Section

Leave a comment