Index
An operator is a symbol that tells the compiler to perform a specific operation. C language has the following types of operators −
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition Operator | Combines two values, like adding apples to your basket. | 5 + 3 gives 8 |
- | Subtraction Operator | Takes away the second value from the first, like spending money from your wallet. | 10 - 6 gives 4 |
* | Multiplication Operator | Multiplies two values, much like scaling up a recipe for more people. | 4 * 3 gives 12 |
/ | Division Operator | Divides the first value by the second, like splitting a pizza among friends. | 20 / 5 gives 4 |
% | Modulus Operator | Finds the remainder after division, like dividing candies evenly but keeping track of leftovers. | 10 % 3 gives 1 |
++ | Increment Operator | Adds one to the value, like pressing a button to move to the next item on a list. | int x = 5; x++ gives 6 |
-- | Decrement Operator | Subtracts one from the value, like pressing a back button to return to the previous step. | int x = 5; x-- gives 4 |
Example :
// Example of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
c = a++;
printf("Incremented val of a");
c = a--;
printf("Decremented val of a");
return 0;
}
// program to check even and odd
#include <stdio.h>
int main()
{
int a;
printf("enter a number \n");
scanf("%d", &a);
if (a % 2 == 0)
{
printf("%d is even number\n", a);
}
else
{
printf("%d is odd number \n", a);
}
return 0;
}
// OUTPUT:
enter a number: 5
5 is odd number
Relational Operator
Relational operators are used to perform various relational operations in C programming.
Definition: Relational operators are binary meaning they require two operands. Relational operators have left to right associativity.
Operator | Meaning | Description | Example |
---|---|---|---|
== | Equal to | Checks if both values are the same. Think of it as asking, “Are they equal?” | 4 == 2 is 0 (false) |
> | Greater than | Compares if the left value is larger than the right. Like asking, “Is it bigger?” | 4 > 2 is 1 (true) |
< | Less than | Compares if the left value is smaller than the right. Think, “Is it smaller?” | 4 < 2 is 0 (false) |
!= | Not equal to | Checks if the values are different. Equivalent to “Are they not the same?” | 4 != 2 is 1 (true) |
>= | Greater than or equal to | Checks if the left value is larger than or exactly equal to the right. | 4 >= 2 is 1 (true) |
<= | Less than or equal to | Checks if the left value is smaller than or exactly equal to the right. | 4 <= 2 is 0 (false) |
Quick Summary:
- 1 = True: Condition is met.
- 0 = False: Condition is not met.
Let’s start with example;
#include <stdio.h>
int main()
{
// realtional operator
int a = 10;
int b = 5;
printf("a==b = %d \n", a == b);
printf("a!=b = %d\n", a != b);
printf("a>b = %d \n", a > b);
printf("a<b = %d \n", a < b);
return 0;
}
OUTPUT:
a==b = 0
a!=b = 1
a>b = 1
a<b = 0
Logical operator
They are used to combine two or more conditions or to complement the evaluation of the original condition under consideration. Each type of logical operator are discussed below:
- 3.1 Logical AND operator: The ‘&&’ operator returns true when both the conditions are satisfied. Otherwise, it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).
- Logical OR operator: The ‘||’ operator returns true even if one (or both) of the conditions are satisfied. Otherwise, it returns false. For example, a || b returns true if one of a or b, or both are true (i.e. non-zero). Of course, it returns true when both a and b are true.
- Logical NOT operator: The ‘!’ operator returns true if the condition is not satisfied. Otherwise, it returns false. For example, !a returns true if a is false, i.e. when a=0.
Lets understand with example:
Example: Write a C program by using all logical operations.
#include <stdio.h>
int main()
{
int a = 10, b = 10, c = 20;
if((a == b) && (c > b)){
printf("(a == b) && (c > b) then true \n");
}
if( (a == b) && (c < b)){
printf("(a == b) && (c < b) then false\n");
}
if( (a == b) || (c < b)){
printf("(a == b) || (c < b) then false\n");
}
if( (a != b) || (c < b)){
printf("(a != b) || (c < b) then false\n");
}
if( !(a != b)){
printf("!(a != b) is then true \n");
}
if(!(a == b)){
printf("!(a == b) is then false\n");
}
return 0;
}
Bitwise operator
Bitwise operators in C perform operations on individual bits of integer data types. Here’s a table describing each bitwise operator, its name, and a description of what it does, along with an example.
Operator | Name | Description | Example | Explanation |
---|---|---|---|---|
& | AND | Performs a bitwise AND on each bit of two integers. Only 1 if both bits are 1. | 5 & 3 | 5 (0101) & 3 (0011) = 1 (0001) |
| | OR | Performs a bitwise OR on each bit of two integers. 1 if either bit is 1. | 5 | 3 | 5 (0101) | 3 (0011) = 1 (0111) |
^ | XOR | Performs a bitwise XOR. 1 if bits are different; 0 if they are the same. | 5 ^ 3 | 5 (0101) ^ 3 (0011) = 6 (0110) |
~ | NOT | Flips all bits (1 becomes 0, and 0 becomes 1). | ~5 | 5 (0101) becomes -6 in two’s complement |
<< | Left Shift | Shifts bits of the number to the left by a specified number of positions, filling with 0s. | 5 << 1 | 5 (0101) << 1 = 10 (1010) |
>> | Right Shift | Shifts bits of the number to the right by a specified number of positions, filling with sign bits. | 5 >> 1 | 5 (0101) >> 1 = 2 (0010) |
Now look at on truth table so that you can understand smoothly:
A | B | A&B | A|B | A^B |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
Let’s go through each operation in a simple way:
Example Code Using Bitwise Operators
#include <stdio.h>
int main() {
int a = 5; // 5 in binary is 0101
int b = 3; // 3 in binary is 0011
printf("a & b = %d\n", a & b); // AND: 0101 & 0011 = 0001, which is 1
printf("a | b = %d\n", a | b); // OR: 0101 | 0011 = 0111, which is 7
printf("a ^ b = %d\n", a ^ b); // XOR: 0101 ^ 0011 = 0110, which is 6
printf("~a = %d\n", ~a); // NOT: ~0101 = 1010 (two's complement), which is -6
printf("a << 1 = %d\n", a << 1); // Left Shift: 0101 << 1 = 1010, which is 10
printf("a >> 1 = %d\n", a >> 1); // Right Shift: 0101 >> 1 = 0010, which is 2
return 0;
}
a & b = 1
a | b = 7
a ^ b = 6
~a = -6
a << 1 = 10 a >> 1 = 2
Explanation of the Output:
a & b = 1
: The bitwise AND operation on 5 (0101
) and 3 (0011
) gives0001
, which is1
.a | b = 7
: The bitwise OR operation on 5 (0101
) and 3 (0011
) gives0111
, which is7
.a ^ b = 6
: The bitwise XOR operation on 5 (0101
) and 3 (0011
) gives0110
, which is6
.~a = -6
: The bitwise NOT operation on5
(represented in binary as0101
) flips all bits to1010
in two’s complement, which is-6
.a << 1 = 10
: Left shifting 5 (0101
) by 1 position results in1010
, which is10
.a >> 1 = 2
: Right shifting 5 (0101
) by 1 position results in0010
, which is2
.
Bitwise Operators Made Easy
Using OR (|
) to Set a Bit
Imagine you have a row of switches (bits) and you want to turn one of them ON (set it to 1
) without affecting the others.
Example:
int value = 0b0000; // All bits are OFF (0)
value |= (1 << 2); // Turn ON the bit at position 2
// Now, value is 0b0100, only the bit at position 2 is ON
Explanation
OR (|
) is like saying, “If either bit is 1
, the result will be 1
.” So this operation ensures the bit at position 2
is 1
and leaves all other bits as they are.
Starting Point:
int value = 0b0000; // All bits are OFF (0)
value
is set to0b0000
, which means all bits are OFF (set to0
).
Goal: Turn ON the Bit at Position 2
- We want to turn ON just one specific bit — the bit at “position 2” (counting starts from 0 on the right).
- So, we want
value
to look like0b0100
where only the third bit from the right is ON.
The Trick: Use 1 << 2
to Create a Bit Mask
(1 << 2)
means “take the number1
and shift it left by 2 positions.”- The binary
1
is0b0001
, but when we shift it left by 2, it becomes0b0100
.
The OR Operation: value |= (1 << 2);
|=
is an OR assignment, which says, “Setvalue
to the result ofvalue | (1 << 2)
.”- OR (
|
) compares each bit and turns the bit ON if at least one of the inputs is1
.
Let’s see how it works:
0000 (original value, in binary)
| 0100 (the bit mask created by 1 << 2)
--------
0100 (new value after OR operation)
Result
value
is now0b0100
, which means only the bit at position 2 is ON.
Using AND (&
) to Clear a Bit
Imagine you want to turn OFF a specific switch (set a bit to 0
), but you don’t want to touch the other switches.
Example:
int value = 0b0111; // Bit at position 2 is ON
value &= ~(1 << 2); // Turn OFF the bit at position 2
// Now, value is 0b0011, the bit at position 2 is OFF
Explanation
By using AND (&
) with a mask that has 0
in the bit position you want to clear, you ensure that bit turns OFF, while other bits stay the same.
Starting Point:
int value = 0b0111; // Bit at position 2 is ON
value
starts as0b0111
, which means the bits look like this: 0 1 1 1- In binary, position 2 (third bit from the right) is ON (1).
Goal: Turn OFF the Bit at Position 2
- We want to change
value
to 0 0 1 1 (binary0b0011
), which has the bit at position 2 turned OFF.
Create a Mask Using ~(1 << 2)
(1 << 2)
means “take the number1
(binary0b0001
) and shift it left by 2 positions.”- When we shift
1
left by 2, it becomes0b0100
. - Now, we use
~
(bitwise NOT) to flip all bits of0b0100
, creating0b1011
. This mask has a0
only at position 2 and1
s everywhere else.
The AND Operation: value &= ~(1 << 2);
&=
is an AND assignment, which says “Setvalue
to the result ofvalue & ~(1 << 2)
.”- AND (
&
) compares each bit and keeps it ON (1) only if both corresponding bits are1
.
Let’s look at it in action:
0111 (original value, in binary)
& 1011 (the mask created by ~(1 << 2))
--------
0011 (new value after AND operation)
Result
value
is now0b0011
, which means the bit at position 2 is turned OFF.
Using XOR (^
) to Toggle a Bit
- Imagine you want to flip a switch to its opposite position: if it’s ON, turn it OFF, and if it’s OFF, turn it ON.
Example
int value = 0b0101; // Bit at position 2 is ON
value ^= (1 << 2); // Flip the bit at position 2
// Now, value is 0b0001, the bit at position 2 is OFF
Explanation
OR (|
) is like saying, “If either bit is 1
, the result will be 1
.” So this operation ensures the bit at position 2
is 1
and leaves all other bits as they are.
Starting Point:
int value = 0b0101; // Bit at position 2 is ON
value
starts as0b0101
, which looks like this in binary: 0 1 0 1- In this case, the bit at position 2 (third bit from the right) is ON (1).
Goal: Flip the Bit at Position 2
- We want to change
value
so that the bit at position 2 is OFF (0). - Flipping means switching 1 to 0 or 0 to 1.
Create the Mask with (1 << 2)
(1 << 2)
means “take the number1
(binary0b0001
) and shift it left by 2 positions.”- Shifting
1
left by 2 gives0b0100
. - This mask (
0b0100
) has a1
only at position 2.
Use XOR (^
) to Flip the Bit
value ^= (1 << 2);
applies the XOR (exclusive OR) operation.- XOR flips a bit only if the mask bit is
1
; otherwise, it leaves it the same. - This means that if the bit in
value
is1
, XOR will change it to0
. If it’s0
, XOR will change it to1
.
Here’s how it looks in action:
0101 (original value, in binary)
^ 0100 (mask created by (1 << 2))
--------
0001 (new value after XOR operation)
Result:
value
is now0b0001
, which means the bit at position 2 is now OFF.
Using NOT (~
) to Invert All Bits
- Imagine you want to flip all switches in a row: every ON switch becomes OFF, and every OFF switch becomes ON.
Example
int value = 0b0101; // Initial value
value = ~value; // Flip all bits
// Now, value is 0b1010 (inverted)
Explanation
NOT (~
) flips every bit in the number, so all 1
s become 0
s and all 0
s become 1
s.
Output
Assignment operator
Assignment operators in C are used to assign values to variables. Here’s a table that explains each operator with examples to illustrate how they work:
Operator | Name | Description | Example | Explanation |
---|---|---|---|---|
= | Simple Assignment | Assigns the right operand’s value to the left operand. | x = 5; | Sets x to 5 . |
+= | Addition Assignment | Adds the right operand to the left operand and assigns the result to the left operand. | x += 3; | Equivalent to x = x + 3 . Adds 3 to x . |
-= | Subtraction Assignment | Subtracts the right operand from the left operand and assigns the result to the left operand. | x -= 2; | Equivalent to x = x - 2 . Subtracts 2 from x . |
*= | Multiplication Assignment | Multiplies the left operand by the right operand and assigns the result to the left operand. | x *= 4; | Equivalent to x = x * 4 . Multiplies x by 4 . |
/= | Division Assignment | Divides the left operand by the right operand and assigns the result to the left operand. | x /= 2; | Equivalent to x = x / 2 . Divides x by 2 . |
%= | Modulus Assignment | Calculates the remainder of division of the left operand by the right operand and assigns to left. | x %= 3; | Equivalent to x = x % 3 . Stores remainder of x divided by 3 . |
Example Code Using Assignment Operators
#include <stdio.h>
int main() {
int x = 10;
x += 5; // x is now 15 (10 + 5)
printf("After += 5, x = %d\n", x);
x -= 3; // x is now 12 (15 - 3)
printf("After -= 3, x = %d\n", x);
x *= 2; // x is now 24 (12 * 2)
printf("After *= 2, x = %d\n", x);
x /= 4; // x is now 6 (24 / 4)
printf("After /= 4, x = %d\n", x);
x %= 5; // x is now 1 (6 % 5)
printf("After %= 5, x = %d\n", x);
return 0;
}
Explanation of Output:
- After
x += 5;
,x
becomes15
. - After
x -= 3;
,x
becomes12
. - After
x *= 2;
,x
becomes24
. - After
x /= 4;
,x
becomes6
. - After
x %= 5;
,x
becomes1
.