Blogware Logo
Blogware
Home
Celebriware
Feedback
Need Help?
ImageSurajit Nandy
Published on : 30th Sep 2021
Cover Image
Operators in C programming
Operators is an important concept in any programming.
There are three types of operators in C programming. They are Unary, Binary and Ternary.
  • Arithmetic Operators :
+, - , *, /, %
  • Assignment Operator :
=
  • Relational Operators :
<, >, <=, >=, ==, !=
  • Logical Operators :
||, &&, !
  • Short-circuit Operators :
|| , &&
  • Bitwise Operators :
~, |, &, ^, <<, >>
  • Compound Assignment Operators or Short-hand Operators :
+=, -=, *=, /=, %=, |=, &=, ^=, <<=, >>=
  • Conditional or Ternary Operator :
? :
  • Increment and Decrement Operators :
++, --
  • Misc. Operators :
*, *., []
Let's discuss the operators available in C programming one by one.
  • Arithmetic Operators :
Source Code :
#include <stdio.h> int main(int argc, char** argv) { int num1 = 10; int num2 = 20; int add = num1 + num2; int sub = num1 - num2; int mul = num1 * num2; float div = num1 / (float)num2; int rem = num2 % num1; printf("Addition = %d\n", add); printf("Subtraction = %d\n", sub); printf("Multiplication = %d\n", mul); printf("Division = %f\n", div); printf("Remainder = %d\n", rem); return 0; }
Output :
Addition = 30 Subtraction = -10 Multiplication = 200 Division = 0.500000 Remainder = 0
  • Assignment Operator :
Source Code :
#include <stdio.h> int main(int argc, char** argv) { int num1 = 10; printf("Assigned in num1 is %d.", num1); return 0; }
Output :
Assigned in num1 is 10.
  • Relational Operators :
Source Code :
#include <stdio.h> int main(int argc, char** argv) { int num1 = 10; int num2 = 11; printf("Greater than = %d\n", num1 > num2); printf("Less than = %d\n", num1 < num2); printf("Greater than or equal to = %d\n", num1 >= num2); printf("Less than or equal to = %d\n", num1 <= num2); printf("Equal to = %d\n", num1 == num2); printf("Not equal to = %d\n", num1 != num2); return 0; }
Output :
Greater than = 0 Less than = 1 Greater than or equal to = 0 Less than or equal to = 1 Equal to = 0 Not equal to = 1
Note : Boolean in C programming is represented as 0(false) and 1(true).
  • Logical Operators :
Source Code :
#include <stdio.h> int main(int argc, char** argv) { int num1 = 10; int num2 = 11; printf("Logical or = %d\n", num1 == 11 || num2 == 11); printf("Logical and = %d\n", num1 == 11 && num2 == 11); printf("Logical not = %d\n", !num1); return 0; }
Output :
Logical or = 1 Logical and = 0 Logical not = 0
Explanation :
As num1 is not 11 so control goes to num2 == 11. The expression is true. For that, 1 is printed.
As num1 is not 11 so control doesn't go to num2 == 11 though the expression is true. For that, it is known as short-circuit operator. Hence, 0 is printed.
Any non-zero value is treated as true. Now logical not operator reverses true as false and false as true. Hence, 0 is printed.
  • Short-circuit Operators :
See the explanation of logical operators.
  • Bitwise Operators :
Source Code :
#include <stdio.h> int main(int argc, char** argv) { int num1 = 10; int num2 = 20; printf("Bitwise not = %d\n", ~num1); printf("Bitwise or = %d\n", num1 | num2); printf("Bitwise and = %d\n", num1 & num2); printf("Bitwise xor = %d\n", num1 ^ num2); printf("Bitwise left shift = %d\n", num1 << 1); printf("Bitwise right shift = %d\n", num1 >> 1); return 0; }
Output :
Bitwise not = -11 Bitwise or = 30 Bitwise and = 0 Bitwise xor = 30 Bitwise left shift = 20 Bitwise right shift = 5
Explanation :
~(pronounce as tilde) is 1's complement operator.
We will use binary representation that computer uses.
Number: 0011
1's complement of the number: 1100
Or(|) operations :
op1 op2 Result (op1 | op2)
0 0 0
0 1 1
1 0 1
1 1 1

And(&) operations :
op1 op2 Result (op1 & op2)
0 0 0
0 1 0
1 0 0
1 1 1

Xor(^) operations :
op1 op2 Result (op1 ^ op2)
0 0 0
0 1 1
1 0 1
1 1 0

Left Shift operator (<<) shifts one bit to the left. The no. of times shifts, it is multiplied by 2 with the number.
Number : 10
Number << 1 : 20

Right Shift operator (>>) shifts one bit to the right. The no. of times shifts, it is divided by 2 with the number.
Number : 10
Number >> 1 : 5
  • Compound Assignment Operators or Short-hand Operators :
Source Code :
#include <stdio.h> int main(int argc, char** argv) { int num1 = 5; int num2 = 20; num1 += num2; // num1 = num1 + num2 printf("Addition = %d\n", num1); num1 |= num2; // num1 = num1 | num2 printf("Bitwise or = %d\n", num1); return 0; }
Output :
Addition = 25 Bitwise or = 29
  • Conditional or Ternary Operator :
#include <stdio.h> int main(int argc, char** argv) { int num1 = 5; int num2 = 20; int big = num1 >= num2 ? num1 : num2; printf("Big = %d\n", big); return 0; }
Output :
Big = 20
Explanation :
Syntax: Condition > evaluate if the condition is true : evaluate if the condition is false.
  • Increment and Decrement Operators :
Source Code :
#include <stdio.h> int main(int argc, char** argv) { int val = 0; printf("Pre increment = %d\n", ++val); printf("Post increment = %d\n", val++); printf("Pre decrement = %d\n", --val); printf("Post decrement = %d\n", val--); return 0; }
Output :
Pre increment = 1 Post increment = 1 Pre decrement = 1 Post decrement = 1
Explanation :
pre increment or decrement operator : first increment or decrement then use.
res = ++pre evaluated as
pre = pre + 1;
res = pre;

post increment or decrement operator : first use then increment or decrement.
res = post++ evaluated as
res = post;
post = post + 1;
  • Misc. Operators:
We have miscellaneous operators in C. We will learn them when we will progress.
Example: [] is subscript operator. * is pointer or value at address operator.
CelebriwareProductsServicesAdvertisementInvestorsPrivacy PolicyTerms of Service
Blogware Logo
Copyright © 2025 Blogware. All rights reserved.