Operators in C programming: A Complete Tutorial

Operators in C programming: A Complete Tutorial

8 mins read510 Views Comment
Updated on Oct 3, 2023 11:48 IST

Here we will explore operators in C programming. Also, you will learn about classification of operators with the help of examples.

2023_01_Operators-in-C-programming.jpg

Being a C programming learner always requires practicing the programs as much as possible, as it will boost programmers to learn more and more in programming. The best way to kick-start your programming journey is to start writing basic mathematical programs to learn deeply about tokens, keywords, and functions.  Here we will learn about operators in C programming in detail.

Also, Explore Data Types in C. 

Check out: C Programming Online Courses & Certifications

Table of Content

  1. Operators in C programming
  2. Classification of Operators 
  3. Operator precedence in C

Explore programming courses

Operators in C programming

Some special symbols used to perform specific operations like arithmetic, logical, relational etc are known as operators. 

These symbols are used between variables to compute mathematical and logical computations. An operator operates on a variable. Let’s understand this with an example 

Example

                               z= x-y;

Here in this example symbol “-” is a subtraction operator that subtracts y from x and stores the evaluated value in the variable z. Here “=” is an assignment operator. x and y are two operands. 

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
50 hours
– / –
40 hours
– / –
2 months
– / –
4 months
5.5 K
140 hours

Classification of Operators 

Operators in c programming are mainly classified into two types that are unary and binary operators, other than that more operators are used which we will discuss later. 

Unary Operators

The operator which operates on a single operand is called a unary operator. Examples- 

  • Increment operator 
  • Decrement operator 
  • size of operator

Yoy can also explore: Understanding Data Structures in C: Types And Operations

Binary Operators

Operators who operate on two or more than two operands are called binary operators. 

  • logical operator (&&,||,!)
  • Bitwise operator (&,|,<<,>>,~,^)
  • Arithmetic operator (+,-,/,*,%)
  • Relational operator(<,>,>=,<=,==,!=)
  • Assignment operator (=,+=,-=,/=,*=,%=)
    Operation Type       Operator Type         Operators 
    Unary operators Increment operator
  Decrement operator ++
  Arithmetic operator +,-,/,*,%
  Binary operators Bitwise operator &,|,<<,>>,~,^
  Relational operator <,>,>=,<=,==,!=
  Assignment operator =,+=,-=,/=,*=,%=
  logical operator &&,||,!
Ternary operators Conditional operator ?:

You must explore: Difference Between C and C++

Unary Operators

Increment operator

An increment operator is a unary operator that operates only on one operand. Increment operators increase the value of a constant or variable by one. 


 
#include <stdio.h>
int main()
{
int x = 12;
float y = 12.5;
printf("++x = %d \n", ++x);
printf("++y = %f \n", ++y);
return 0;
}
Copy code

Output-

++x= 13
++y=13.50000

Learn more about Data Types in C Programming With Examples

Decrement Operator

The decrement operator is a unary operator that operates only on one operand. Decrement operators decrease the value of a constant or variable by one. 


 
#include <stdio.h>
int main()
{
int x = 12;
float y = 12.5;
printf("--x = %d \n", --x);
printf("--y = %f \n", --y);
return 0;
}
Copy code

Output

 --x=11
--y=11.5

sizeof operator

the sizeof operator is a unary operator and operates only on a single operand. sizeof() operator is used in many ways, in this program, we use it with data type to know the allocated memory of that data type. 


 
#include <stdio.h>
int main()
{
printf("%lu\n", sizeof(unsigned));
printf("%lu\n", sizeof(int));
printf("%lu\n", sizeof(float));
printf("%lu", sizeof(signed));
return 0;
}
Copy code

Output 

4
4
4
4

You can also explore: Implementing arrays in C programming

Binary Operators

Logical Operator

 If an expression contains any logical operator in it, it will always return 0 or 1 based on the result true or false of that expression. 

Three types of logical operators, AND, OR and NOT, are commonly used in decision-making in c programming. 

        AND (&&) True, only if all expressions are true
          OR (||) True, only if one expression is true.
          NOT(!) True, only if the expression is 0

C program to demonstrate the use of logical AND operator 


 
#include <stdio.h>
int main()
{
int x = 5, y = 5, z = 10, outcome;
outcome = (x == y) || (z > y);
printf("(x == y) || (z > y) is %d \n", outcome);
outcome = (x == y) || (x < y);
printf("(x == y) || (x < y) is %d \n", outcome);
return 0;
}
Copy code

Output –

(x == y) || (z > y) is 1 
(x == y) || (x < y) is 1 

C program to demonstrate the use of logical OR operator 


 
#include <stdio.h>
int main()
{
int x = 5, y = 5, z = 10, outcome;
outcome = !(x != y);
printf("!(x != y) is %d \n", outcome);
outcome = !(x == y);
printf("!(x == y) is %d \n", outcome);
return 0;
}
Copy code

Output –

!(x != y)  is 1 
!(x == y) is 0 

Bitwise Operator

 Bitwise operators perform operations on bit patterns at the binary level. These operations are performed on single bits. 

 Bitwise operators perform operations on bit patterns at the binary level. These operations are performed on single bits. 

Operator symbol Operator name
 |  Bitwise OR
 ^  Bitwise XOR
 ~  Bitwise Compliment
 &  Bitwise AND
 <<   Shift Left
 >>   Shift Right

Arithmetic operator

Arithmetic operators are used to perform mathematical operations on two or more operands. To perform arithmetic functions 5 operators (*,-,+,/,%) are used in c programming.

   Subtraction
 +  Addition
 /  Division
 *  Multiplication
 %  Modulus

C program to demonstrate the subtraction operation 


 
#include<stdio.h>
#include<conio.h>
int main()
{
int x, y, sub;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of y: ");
scanf("%d", &y);
sub = x - y;
printf("Subtraction of %d and %d is %d.",x,y, sub);
return 0;
}
Copy code

Output-

Enter the value of x: 6
Enter the value of y: 2
Subtraction of 6 and 2 is 4.

Addition operation (+)

This operation is used to add two data items. 

C program to demonstrate the Addition operation 


 
#include<stdio.h>
#include<conio.h>
int main()
{
int x, y, add;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of y: ");
scanf("%d", &y);
add = x + y;
printf("Sum of %d and %d is %d.",x,y, add);
return 0;
}
Copy code

Output- 

Enter the value of x: 5
Enter the value of y: 6
Sum of 5 and 6 is 11.

You can also explore: Storage classes in C

Division operation (/)

Division operator is use to divide one element from other. 

C program to demonstrate the Division operation 


 
#include<stdio.h>
#include<conio.h>
int main()
{
int x, y, Division;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of y: ");
scanf("%d", &y);
Division = x/y;
printf("Division of %d and %d is %d.",x,y, Division);
return 0;
}
Copy code

Output-


 
Enter the value of x: 16
Enter the value of y: 8
Division of 16 and 8 is 2.
Copy code

You can also explore: What is Do While Loop in C?

Multiplication operation (*)

To multiply one element to another, this operator is used. 

C program to demonstrate the multiplication operation


 
#include<stdio.h>
#include<conio.h>
int main()
{
int x, y, product;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of y: ");
scanf("%d", &y);
product = x*y;
printf("product of %d and %d is %d.",x,y, product);
return 0;
}
Copy code

Output

Enter the value of x: 5
Enter the value of y: 6
product of 5 and 6 is 30.

Modulus operation (%)

Modulo operators return the reminder of an element when it is divided by another element. 

C program to demonstrate the Modulus operation 


 
#include<stdio.h>
#include<conio.h>
int main()
{
int x, y, mod;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of y: ");
scanf("%d", &y);
mod= x%y;
printf("remainder %d", mod);
return 0;
}
Copy code

Output-

Enter the value of x: 15
Enter the value of y: 7
remainder 1
Prime Number in C: A Beginner’s Guide
Factorial Programs in C – A Step-by-Step Guide
File Handling in C

Relational Operator

 As it is clear from its name relational operator focuses on the relation between the two operands; if the relation is true, it returns 1, and if the relation is false, it returns 0. The below table depicts the relational operator symbols and their description.


 Less than
 >   Greater than
 !=  Not equal to
 >=  Greater than or equal to
 <= Less than or equal to
 ==  Equal to

C program to demonstrate the use of less than/ greater than operator 


 
#include <stdio.h>
int main()
{
int x = 10, y = 10, z = 25;
printf("%d > %d is %d \n", x, y, x > y);
printf("%d > %d is %d \n", x, z, x > z);
printf("%d < %d is %d \n", x, y, x < y);
printf("%d < %d is %d \n", x, z, x < z);
return 0;
}
Copy code

Output

10 > 10 is 0 
10 > 25 is 0 
10 < 10 is 0 
10 < 25 is 1 

C program to demonstrate the use of equal to/not equal to operator 


 
#include <stdio.h>
int main()
{
int x = 10, y = 10, z = 25;
printf("%d == %d is %d \n", x, y, x == y);
printf("%d == %d is %d \n", x, z, x == z);
printf("%d != %d is %d \n", x, y, x != y);
printf("%d != %d is %d \n", x, z, x != z);
return 0;
}
Copy code

Output


 
10 == 10 is 1
10 == 25 is 0
10 != 10 is 0
10 != 25 is 1
Copy code

C program to demonstrate the use of greater than or equal to/less than or equal to operator 


 
#include <stdio.h>
int main()
{
int x = 10, y = 10, z = 25;
printf("%d >= %d is %d \n", x, y, x >= y);
printf("%d >= %d is %d \n", x, z, x >= z);
printf("%d <= %d is %d \n", x, y, x <= y);
printf("%d <= %d is %d \n", x, z, x <= z);
return 0;
}
Copy code

Output

10 >= 10 is 1 
10 >= 25 is 0 
10 <= 10 is 1 
10 <= 25 is 1 

Assignment Operator

The name of this operator clearly defines its work, the assignment operator is used for assigning a value to a variable. The assignment operator and its example are given below in this table. 

Here, x+=y is same as x=x+y

 +=  x+=y  x=x+y
 -=  x-=y  x=x-y
 /=  x/=y  x=x/y
 %=  x%=y   x=x%y
 *=  x*=y   x=x*y
 =  x=y  x=y

C program to demonstrate the use of assignment operator 


 
#include <stdio.h>
int main()
{
int x = 25, y;
y = x;
printf("y = %d\n", y);
y += x;
printf("y = %d\n", y);
y -= x;
printf("y = %d\n", y);
y*= x;
printf("y = %d\n", y);
y /= x;
printf("y = %d\n", y);
y %= x;
printf("y = %d\n", y);
return 0;
}
Copy code

Output

y = 25
y = 50
y = 25
y = 625
y = 25
y = 0

Operators Precedence in C

Operator precedence decides how a mathematical expression is evaluated. Let’s understand it with an example of a mathematical equation. 

a= 17+3*20

Here in this equation, the value of “a” will be evaluated as 77, not 400, because the precedence of the multiplication operator is higher than the addition operator. In the below table, precedence increases from top to bottom. 

precedence Operator symbol Operator name Associativity
1 ++ — -> . () [] Suffix/postfix increment decrement, structure, function call, array subscribing  Left to right
2 sizeof & +- !~ sizeof, address of, unary plus and minus, logical not, and bitwise not  Right to left
3 % / * Remainder, division, multiplication  
4 –          + Subtraction, addition  
5 >> << Bitwise right shift, bitwise left shift  
6 <=<  >>= Less than or equal to , less than , greater than or greater than or equal to  Left to right
7 != == Not equal to, equal to  
8 & Bitwise AND  
9 ^ Bitwise XOR  
10 | Bitwise OR  
11 && Logical AND  
12 || Logical OR  
13 ?: Conditional operator  
14 =-= +=%= /= *=>>=  <<=&= |= ^= Assignment, Assignment by addition, subtraction, modulus, division, multiplication assignment by bitwise right shift, left shift, AND, OR and XOR Right to left
15 , Comma Left to right

Also Read: Top C Programming Interview Questions and Answers

Conclusion

Practicing makes a man perfect if a learner wants to master the mother of all languages, that is C, then, it’s mandatory to know deeply about the basic programs of operators in C. In a nutshell, this article will aid and support every individual’s c learning journey.

Contributed By: Srashti Soni

FAQs

Q1 What is operator precedence in c?

Answer-Operator precedence decides how a mathematical expression is evaluated.

Q2 What is the use of the bitwise operator in c programming?

Answer- Bitwise operators perform operations on bit patterns at the binary level.

Q3 What is the ++ operator in C?

Answer- ++ (the increment operator) increase the value of the operand by 1.

Q4 What is the binary operator?

Answer- Those operators that operate on two or more operands are called binary operators.

Q5 What is a unary operator in C?

Answer- Those operators that operate on one operand are called binary operators.

Q6 Are addition and multiplication equal in precedence?

Answer- No, multiplication has high precedence than addition.

Q7 Which prefix has the higher precedence?

Answer- Unary Increment operator

Q8 What is the symbol of the conditional operator?

Answer- ?:

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio