Operators in C
Welcome to this comprehensive guide on Operators in C. If you’re looking to increase your understanding of this essential aspect of the C language, you’ve come to the right place. We’ll cover the basics and dive into the details, all while keeping things engaging and easy to understand. So, let’s get started!
Table of Contents
Types of Operators in C
Operators in C are the building blocks that allow us to perform various operations on our data. They’re like the tools in a toolbox, each with a specific purpose and function. Let’s take a look at the different types of operators in C.
Arithmetic Operators
Arithmetic operators are the bread and butter of any programming language. They allow us to perform basic mathematical operations like addition, subtraction, multiplication, and division. They also include the modulus operator, which gives the remainder of a division operation.
Here’s a table to illustrate:
Operator  Description  Example  Result 

+  Addition  a + b  Sum of a and b 
  Subtraction  a  b  Difference of a and b 
*  Multiplication  a * b  Product of a and b 
/  Division  a / b  Quotient of a and b 
%  Modulus  a % b  Remainder of a / b 
Think of them as the basic tools in your coding toolbox, the hammer and nails of the programming world. Without them, we wouldn’t be able to do much. Let’s see them in action:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("a + b = %d\n", a + b); // Outputs: a + b = 13
printf("a  b = %d\n", a  b); // Outputs: a  b = 7
printf("a * b = %d\n", a * b); // Outputs: a * b = 30
printf("a / b = %d\n", a / b); // Outputs: a / b = 3
printf("a %% b = %d\n", a % b); // Outputs: a % b = 1
}
CRelational Operators
Relational operators are used to compare two values. They return 1 if the comparison is true, and 0 if it’s false. They’re like the judges of the programming world, always comparing and deciding.
Here’s a table to illustrate:
Operator  Description  Example  Result 

==  Equal to  a == b  1 if a is equal to b , else 0 
!=  Not equal to  a != b  1 if a is not equal to b , else 0 
>  Greater than  a > b  1 if a is greater than b , else 0 
<  Less than  a < b  1 if a is less than b , else 0 
>=  Greater than or equal to  a >= b  1 if a is greater than or equal to b , else 0 
<=  Less than or equal to  a <= b  1 if a is less than or equal to b , else 0 
They’re like the magnifying glass of the programming world, allowing us to examine our data and make decisions based on it. Let’s put them under the microscope:
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("a == b: %d\n", a == b); // Outputs: a == b: 0
printf("a != b: %d\n", a != b); // Outputs: a != b: 1
printf("a > b: %d\n", a > b); // Outputs: a > b: 0
printf("a < b: %d\n", a < b); // Outputs: a < b: 1
printf("a >= b: %d\n", a >= b); // Outputs: a >= b: 0
printf("a <= b: %d\n", a <= b); // Outputs: a <= b: 1
}
CLogical Operators
Logical operators are used to perform logical operations like AND, OR, and NOT. They’re like the philosophers of the programming world, always pondering truth and falsehood.
Here’s a table to illustrate:
Operator  Description  Example  Result 

&&  Logical AND  a && b  1 if both a and b are true, else 0 
  Logical OR  a  b  1 if either a or b is true, else 0 
!  Logical NOT  !a  1 if a is false, else 0 
They’re like the traffic lights of the programming world, directing the flow of our code based on certain conditions. Let’s see how they work:
#include <stdio.h>
int main() {
int a = 1, b = 0;
printf("a && b: %d\n", a && b); // Outputs: a && b: 0
printf("a  b: %d\n", a  b); // Outputs: a  b: 1
printf("!a: %d\n", !a); // Outputs: !a: 0
}
CAssignment Operators
Assignment operators in C are used to assign values to variables. The most basic assignment operator is =
, but there are also compound assignment operators that perform an operation and an assignment in one step. Let’s take a closer look:
Operator  Description  Example  Result 

=  Assigns the value from the right operand to the left operand.  a = b  a is now equal to b 
+=  Adds the right operand to the left operand and assigns the result to the left operand.  a += b  Equivalent to a = a + b 
=  Subtracts the right operand from the left operand and assigns the result to the left operand.  a = b  Equivalent to a = a  b 
*=  Multiplies the left operand by the right operand and assigns the result to the left operand.  a *= b  Equivalent to a = a * b 
/=  Divides the left operand by the right operand and assigns the result to the left operand.  a /= b  Equivalent to a = a / b 
%=  Takes the modulus of the left operand by the right operand and assigns the result to the left operand.  a %= b  Equivalent to a = a % b 
&=  Performs a bitwise AND on the operands and assigns the result to the left operand.  a &= b  Equivalent to a = a & b 
=  Performs a bitwise OR on the operands and assigns the result to the left operand.  a = b  Equivalent to a = a  b 
^=  Performs a bitwise XOR on the operands and assigns the result to the left operand.  a ^= b  Equivalent to a = a ^ b 
<<=  Performs a left shift on the left operand and assigns the result to the left operand.  a <<= 2  Equivalent to a = a << 2 
>>=  Performs a right shift on the left operand and assigns the result to the left operand.  a >>= 2  Equivalent to a = a >> 2 
Let’s see them in action:
#include <stdio.h>
int main() {
int a = 10, b = 5;
a += b; // a is now 15
printf("a = %d\n", a);
a = b; // a is now 10
printf("a = %d\n", a);
a *= b; // a is now 50
printf("a = %d\n", a);
a /= b; // a is now 10
printf("a = %d\n", a);
a %= b; // a is now 0
printf("a = %d\n", a);
a = 10; // resetting a to 10
printf("a = %d\n", a);
a &= b; // a is now 0
printf("a = %d\n", a);
a = 10; // resetting a to 10
printf("a = %d\n", a);
a = b; // a is now 15
printf("a = %d\n", a);
a ^= b; // a is now 10
printf("a = %d\n", a);
a <<= 2; // a is now 40
printf("a = %d\n", a);
a >>= 2; // a is now 10
printf("a = %d\n", a);
}
CIn this code snippet, we’re performing various assignment operations on the variables a
and b
. We start with the +=
operation, which adds b
to a
and assigns the result to a
. We then do the same for the =
(subtraction), *=
(multiplication), /=
(division), and %=
(modulus) operations. We then perform the bitwise AND (&=
), OR (=
), and XOR (^=
) operations, as well as the left shift (<<=
) and right shift (>>=
) operations. Each operation modifies the value of a
and assigns the result back to a
.
Bitwise Operators
Bitwise operators work at the binary level, dealing directly with the 1s and 0s that make up our data . These include bitwise AND (&
), bitwise OR (
), bitwise XOR (^
), bitwise NOT (~
), left shift (<<
), and right shift (>>
).
They’re like the secret codebreakers of the programming world. Let’s decode their operations:
Operator  Description  Example (Binary)  Result (Binary) 

& (AND)  If both bits are 1, the result is 1. Otherwise, it’s 0.  1 & 1  1 
 (OR)  If either bit is 1, the result is 1. Otherwise, it’s 0.  1  0  1 
^ (XOR)  If the bits are different, the result is 1. Otherwise, it’s 0.  1 ^ 0  1 
~ (NOT)  Flips the bit. If it’s 1, it becomes 0 and vice versa.  ~1  0 
<< (Left Shift)  Shifts the bits of the number to the left and fills 0 on voids left as a result.  2 << 2  8 (In binary, 10 becomes 1000) 
>> (Right Shift)  Shifts the bits of the number to the right and fills 0 on voids left as a result.  8 >> 2  2 (In binary, 1000 becomes 10) 
They’re like the secret agents of the programming world, working behind the scenes on the binary level. Let’s uncover their secrets:
#include <stdio.h>
int main() {
int a = 12; // In binary: 1100
int b = 25; // In binary: 11001
printf("a & b: %d\n", a & b); // Outputs: a & b: 8 (In binary: 1000)
printf("a  b: %d\n", a  b); // Outputs: a  b: 29 (In binary: 11101)
printf("a ^ b: %d\n", a ^ b); // Outputs: a ^ b: 21 (In binary: 10101)
printf("~a: %d\n", ~a); // Outputs: ~a: 13 (In binary: 11110011, in two's complement form)
printf("a << 2: %d\n", a << 2); // Outputs: a << 2: 48 (In binary: 110000)
printf("a >> 2: %d\n", a >> 2); // Outputs: a >> 2: 3 (In binary: 11)
}
CIn this code snippet, we’re performing various bitwise operations on the variables a
and b
. We start by performing a bitwise AND (&
) operation, which compares each bit of a
and b
and sets the corresponding result bit to 1 only if both bits are 1. The bitwise OR (
) operation sets the result bit to 1 if either bit is 1. The bitwise XOR (^
) operation sets the result bit to 1 only if the bits are different. The bitwise NOT (~
) operation flips the bits of a
. The left shift (<<
) operation shifts the bits of a
to the left by two places, and the right shift (>>
) operation shifts the bits of a
to the right by two places.
Unary Operators
Unary operators are the lone wolves of the programming world, working solo on a single operand. These include unary minus (
), unary plus (+
), increment (++
), decrement (
), and the sizeof
operator which returns the size of an operand.
Let’s take a closer look at each of them:
Operator  Description  Example  Result 

 (Unary Minus)  Changes the sign of the operand.  a  If a is 10, a is 10. 
+ (Unary Plus)  Doesn’t change anything, it’s there for symmetry.  +a  If a is 10, +a is also 10. 
++ (PreIncrement)  Increases the value of the operand by 1 before the current expression is evaluated.  ++a  If a is 10, ++a is 11. 
 (PreDecrement)  Decreases the value of the operand by 1 before the current expression is evaluated.  a  If a is 10, a is 9. 
a++ (PostIncrement)  Increases the value of the operand by 1 after the current expression is evaluated.  a++  If a is 10, a++ is 10 in the current expression, but a becomes 11 in the next statement. 
a (PostDecrement)  Decreases the value of the operand by 1 after the current expression is evaluated.  a  If a is 10, a is 10 in the current expression, but a becomes 9 in the next statement. 
sizeof  Returns the size of the operand in bytes.  sizeof(a)  If a is an int , sizeof(a) is typically 4. 
They’re like the lone wolves of the programming world, working solo on a single operand. Let’s see how they operate:
#include <stdio.h>
int main() {
int a = 10;
printf("Unary Minus: %d\n", a); // Outputs: Unary Minus: 10
printf("Unary Plus: %d\n", +a); // Outputs: Unary Plus: 10
printf("PreIncrement: %d\n", ++a); // Outputs: PreIncrement: 11
printf("PreDecrement: %d\n", a); // Outputs: PreDecrement: 10
printf("PostIncrement: %d\n", a++); // Outputs: PostIncrement: 10
printf("Value of a after PostIncrement: %d\n", a); // Outputs: Value of a after PostIncrement: 11
printf("PostDecrement: %d\n", a); // Outputs: PostDecrement: 11
printf("Value of a after PostDecrement: %d\n", a); // Outputs: Value of a after PostDecrement: 10
printf("Sizeof: %lu\n", sizeof(a)); // Outputs: Sizeof: 4
}
CIn this code snippet, we’re performing various unary operations on the variable a
. We start with the unary minus (
) operation, which changes the sign of a
. The unary plus (+
) operation doesn’t change anything, it’s there for symmetry. The preincrement (++
) operation increases the value of a
by 1 before the current expression is evaluated. The predecrement (
) operation decreases the value of a
by 1 before the current expression is evaluated. The postincrement (a++
) operation increases the value of a
by 1 after the current expression is evaluated. The postdecrement (a
) operation decreases the value of a
by 1 after the current expression is evaluated. The sizeof
operation returns the size of a
in bytes.
Wrapping Up
Operators in C are powerful tools that allow us to manipulate our data in various ways. Understanding them is crucial to becoming proficient in C. We hope this guide has helped you understand the different types of operators in C and how to use them. Keep practicing and experimenting with different operators to solidify your understanding.
Frequently Asked Questions

What are operators in C?
Operators in C are symbols that tell the compiler to perform specific mathematical or logical manipulations. They are used in C to perform operations on variables and values.

What are the 8 operators in C program?
The eight types of operators in C are: Arithmetic, Relational, Logical, Bitwise, Assignment, Unary, Ternary, and Comma.

What does &= mean in C?
The operator ‘&=’ is a compound assignment operator in C. It performs a bitwise AND operation on the value of the variable and the value on the right, and then assigns the result to the variable. For example,
a &= b
is equivalent toa = a & b
. 
What are the 7 types of operators?
The seven types of operators in most programming languages, including C, are: Arithmetic, Relational, Logical, Bitwise, Assignment, Unary, and Ternary.

Can you explain more about each of the bitwise operators?
Bitwise operators are a bit like the secret codebreakers of the programming world. They work at the binary level, dealing directly with the 1s and 0s that make up our data. Let’s decode their operations:
Bitwise AND (&
): This operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0. It’s like a strict security guard that only lets you in if both your ID and your face match. Check the example in the section “Bitwise Operators”.
Bitwise OR (
): This operator also compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0. It’s like a lenient security guard that lets you in if either your ID or your face matches. Check the example in the section “Bitwise Operators”.
Bitwise XOR (^
): This operator works like the OR operator, but it sets the result bit to 0 if both bits are 1. It’s like a quirky security guard that lets you in if your ID or your face matches, but not both.
Bitwise NOT (~
): This operator flips the bits of its operand. If a bit is 0, it becomes 1, and if it’s 1, it becomes 0. It’s like a contrarian friend who always takes the opposite view. Check the example in the section “Bitwise Operators”.
Left shift (<<
): This operator shifts the bits of its left operand to the left by the number of places specified by the right operand. New bits on the right are filled with 0s. It’s like moving a line of people to the left, with new people joining from the right. Check the example in the section “Bitwise Operators”.
Right shift (>>
): This operator shifts the bits of its left operand to the right by the number of places specified by the right operand. New bits on the left depend on the type of the left operand (0s for unsigned types, sign bit for signed types). It’s like moving a line of people to the right, with new people joining from the left. Check the example in the section “Bitwise Operators”.
Related Tutorials
If you found this guide helpful, you might also want to check out these related tutorials:
Remember, the key to mastering any programming language is practice. So, keep coding and exploring!