C Operators – Types and Examples

The C programming language comes with very rich built-in functions. There is a vast use of operators in all programming languages. Operators are a useful and powerful feature of the C/C++ programming language. Without them, the functionality of C is useless. It makes it easy for a programmer to write codes very efficiently and easily. 

What are Operators?

An operator is a symbol that operates on a variable or value. It is used for performing certain operations like arithmetical, logical, relational, etc.

When a programmer wants to perform some type of mathematical operation then you have to use operators. The purpose of operators is mainly for mathematical and logical calculations.

Types of Operators in C

In C, there are various types of operators available to perform various operations. Following are the list of operators present in C:-

1. Arithmetic Operators in C

The purpose of this operator is to perform mathematical operations like addition, subtraction, division, multiplication etc.

Operator What it does Example
+ Addition between 2 operands. A + B = 10
Subtraction between 2 operands. A − B = 0
* Multiplication between 2 operands. A * B = 25
/ Division between 2 operands. B / A = 1
% Modulus Operator and remainder of after an integer division. B % A = 0
++ Increases the integer value by one. A++ = 6
Decreases the integer value by one. B– = 4

Example of Arithmetic Operators in C:-

int main()
{
  int a = 10,b = 15, result;
  result = a+b;
  printf("Welcome to TechVidvan Tutorials...\n");  
  printf("Addition: a+b = %d \n",result);
  result = a-b;
  printf("Subtraction: a-b = %d \n",result);
  result = a*b;
  printf("Multiplication: a*b = %d \n",result);
  result = a/b;
  printf("Division: a/b = %d \n",result);
  result = a%b;
  printf("Modulo Division: %d \n",result);
  result = ++a;
  printf("Increment the value of a by 1: %d \n",result);
  result = --b;
  printf("Decremented the value of b by 1: %d \n",result);
  return 0;
}

Output:

Welcome to TechVidvan Tutorials…
Addition: a+b = 25
Subtraction: a-b = -5
Multiplication: a*b = 150
Division: a/b = 0
Modulo Division: 10
Increment the value of a by 1: 11
Decremented the value of b by 1: 14

2. Increment Operator in C

Mainly used for incrementing the value of an integer. It is represented by the ‘++’ operator. ++x will increase the value of the variable x instantly. But if it is placed after the variable then it gets increased before the execution of the next statement.

Example of C Increment Operator

#include <stdio.h>
int main() {
  int a = 10;
  ++a;
  printf("Value of a: %d",a);
  return 0;
}

Output

Value of a: 11

3. Decrement Operator in C

Mainly used for decrementing the value of an integer. It is represented by the ‘–’ operator. –x will decrease the value of the variable x instantly. But if it is placed after the variable then it gets decreased before the execution of the next statement.

Example of C Decrement Operator

#include <stdio.h>
int main() {
  int a = 10;
  --a;
  printf("Value of a: %d",a);
  return 0;
}

Output

Value of a: 9

4. Assignment Operators in C

The purpose of this operator is to assign value to a variable. The most used assignment operator is “=”. 

Operator Example
= a=b or b=a
+= a += b or a = a+b
-= a -=b or a = a-b
*= a *= b or a = a*b
/= a /= b or a = a/b
%= a %= b or a = a%b

Example of Assignment Operators in C:-

#include <stdio.h>
int main()
{
  int a = 99, result;
  result = a;
  printf("Welcome to TechVidvan Tutorials...\n");
  printf("Value of result = %d\n", result);
  result += a;	// or result = result + a
  printf("Value of result = %d\n", result); // After Addition
  result -= a;	// or result = result - a	 
  printf("Value of result = %d\n", result); // After Subtraction
  result *= a;	// or result = result * a	 
  printf("Value of result = %d\n", result); // After Multiplication
  result /= a;	// or result = result / a
  printf("Value of result = %d\n", result);
  return 0;
}

Output:

Welcome to TechVidvan Tutorials…
Value of result = 99
Value of result = 198
Value of result = 99
Value of result = 9801
Value of result = 99

5. Relational Operators in C

Mainly used for checking relationships between operands. With the help of this operator, you can check whether one operand is equal to or greater than the other operand or not.

It returns 1 when the relation is true. And when it is false, it returns 0.

Operator What it does Example
== Equal to 5==5 will be 1
> Greater than 5>6 will be 0
< Less than 6<7 will be 1
>= Greater than equal to 2 >= 1 will be 1
<= Less than equal to 1 <= 2 will be 1
!= Not equal to  5 != 6 will be 1

Example of Relational Operators in C:-

#include <stdio.h>
int main()
{
  int j = 6, t = 4;

  printf("%d == %d is %d \n", j, t, j == t);
  printf("%d > %d is %d \n", j, t, j > t);
  printf("%d < %d is %d \n", j, t, j < t);
  printf("%d != %d is %d \n", j, t, j != t);
  printf("%d >= %d is %d \n", j, t, j >= t);
  printf("%d <= %d is %d \n", j, t, j <= t);

  return 0;
}

Output

6 == 4 is 0
6 > 4 is 1
6 < 4 is 0
6 != 4 is 1
6 >= 4 is 1
6 <= 4 is 0

6. Logical Operators in C

In the C programming language, Logical operators are mostly used for decision making. A logical operator returns either 0 or 1 whether the condition is true or false.

Operator What it does
&& (Logical AND) True only if all conditions satisfy.
|| (Logical OR) True only if either one condition satisfies.
! (Logical Not) True only if the operand is 0.

Example of Logical Operators in C:-

#include <stdio.h>
int main()
{
  int i = 5, j = 5, k = 10, final;

  printf("i is equal to j or k greater than j is is %d \n", (i == j) && (k > j));

  printf("i is equal to j or k less than j is %d \n", (i == j) || (k < j));

  printf("i not equal to j or k less than j is %d \n", (i != j) || (k < j));

  return 0;
}

Output

i is equal to j or k greater than j is 1
i is equal to j or k less than j is 1
i not equal to j or k less than j is 0

7. Conditional Operator in C

Also known as Ternary operator. The main purpose of conditional operators is in decision making statements. It is similar to an if-else statement.

Syntax of a conditional operator:-

Expression1? statement1: statement2; 
  • Expression1 is a boolean expression, it can be either true or false.
  • If the Expression1 returns true then statement1 will get executed.
  • If the Expression1 returns false then statement2 will get executed.

Example of Conditional Operators in C:-

#include <stdio.h>  
int main()  
{  
int number=13;
(number>14)? (printf("It is greater than number 14!")) : (printf("It is less than number 14!"));  // conditional operator  
return 0;  
}

Output

It is less than number 14!

If we set the number to 15 then it will give the output⇒ It is greater than number 14!

8. Bitwise Operator in C

C also provides special operators for bit operation between two variables.

Operator Also known as
<< Binary Left Shift Operator
>> Binary Right Shift Operator
~ Binary Ones Complement Operator
& Binary AND Operator
^ Binary XOR Operator
| Binary OR Operator

Example of Bitwise Operators in C:-

#include <stdio.h>
int main() {
int a = 50;      
int b = 5;    
int result = 0;      	 
result = a & b;   	// Binary AND Operator
printf("Binary AND Operator of a and b is %d\n", result );
result = a ^ b;   	// Binary XOR Operator
printf("Binary XOR Operator of a and b is %d\n", result );
result = ~a;      	// Binary Ones Complement Operator
printf("Binary Ones Complement Operator of a is %d\n", result );
result = a << 2; 	// Binary Left Shift Operator
printf("Binary Left Shift Operator of a is %d\n", result );
result = a >> 2; 	// Binary Right Shift Operator
printf("Binary Right Shift Operator of a is %d\n", result );
}

Output

Binary AND Operator of a and b is 0
Binary XOR Operator of a and b is 55
Binary Ones Complement Operator of a is -51
Binary Left Shift Operator of a is 200
Binary Right Shift Operator of a is 12

9. Misc operators in C

It includes operators like sizeof(), reference operator(&), pointer operator(*), condition operator(?).

Operator Example What it does
sizeof sizeof(a) Returns the size occupied by the data type of the operand. 
& &a Refers to the memory address where operand is stored.
* *a A pointer.
?: a? b: statement Alternative of if-else.

Example:-

#include<stdio.h>
int main()
{
printf("Welcome to TechVidvan tutorials!\n\n");
int number = 10, *pointer;
int another_number=13;
pointer=&number;
printf("Example of pointer and reference operator!\n");
printf("Memory address: %d\n",pointer);
printf("Example of condition operator!\n");
(another_number>14)? (printf("It is greater than number 14!")) : (printf("It is less than number 14!"));
// if you put the value 15 in another_number variable then it will output>> It is greater than number 14!
return 0;
}

Output

Welcome to TechVidvan tutorials!
Example of pointer and reference operator!
Memory address: 1701039932
Example of condition operator!
It is less than number 14!

Special Operators in C

Apart from these operators, C supports special operators:-

1. sizeof():- If you want to check the size of data types available in C then you can do it by using sizeof() operator.

2. Reference Operator (&):– Used for returning the address of a memory location.

3. Pointer Operator (*):– It is a pointer to a variable.

sizeof operator in C

If you want to check the size of data types available in C then you can do it by using sizeof() operator. Below is the code to show the storage sizes of int, char and float data types.

#include <stdio.h>
int main()
{
  printf("Sizes of int, char and float...\n");
  printf("int is: %d bytes\n", sizeof(int));
  printf("char is: %d bytes\n", sizeof(char));
  printf("float is: %d bytes\n", sizeof(float));
  return 0;
}

Output

Sizes of int, char and float…
int is: 4 bytes
char is: 1 bytes
float is: 4 bytes

Comma Operator in C

Mainly used for separating expressions, variable declarations, function calls etc. It works on two operands. It is a binary operator. Comma acts as a separator.

Syntax of comma operator:-

int a=1, b=2, c=3, d=4;

Operator Precedence in C

In between operators, some have higher precedence and some have lower precedence. For example, Division has a higher precedence than subtraction operator.

Operator Precedence determines how the expression is evaluated. 

Category Operator Associativity
Postfix () [] -> . ++ – – Left to right
Unary + – ! ~ ++ – – (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + – Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

Operators which have higher precedence are at the top of the table. And operators which have lower precedence are at the bottom.

Summary

An operator is a symbol which operates on a variable or value. There are types of operators like arithmetic, logical, conditional, relational, bitwise, assignment operators etc. Some special types of operators are also present in C like sizeof(), Pointer operator, Reference operator etc.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.