What is the difference between the ‘=’ and ‘==’ operators?
10.
Conclusion
Table of contents
Master PowerBI: Solve a Netflix Data Case Study
13 Jul, 2024 @ 06:30 AM
Speaker
Ashwin Goyal
Product Manager @
Operators in C are the fundamental elements that drive logic and computation within programs. They are crucial for performing operations like arithmetic, comparison, logical operations, etc., and form the backbone of decisionmaking and problemsolving in programming.
Understanding operators is essential for anyone looking to delve into the world of C programming.
Types of Operators in C are mentioned below:
Arithmetic Operators
Relational Operators
Bitwise Operators
Shift Operators
Ternary or Conditional Operators
Logical Operators
Assignment Operators
Misc Operators
Let's discuss them in detail:
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations: addition (+), subtraction (), multiplication (*), division (/), and modulus (%). These operators are used to calculate and manipulate numerical values in programming and mathematical expressions.
Operator
Description
Example
+
Addition
a + b

Subtraction
a  b
*
Multiplication
a * b
/
Division
a / b
%
Modulus (Remainder)
a % b
For example:
C
C
#include <stdio.h>
int main() { int a = 10, b = 5; printf("Addition: %d\n", a + b); printf("Subtraction: %d\n", a  b); printf("Multiplication: %d\n", a * b); printf("Division: %d\n", a / b); printf("Modulus: %d\n", a % b); return 0; }
Output:
2. Relational Operators
Relational operators compare two values or expressions:
Equal to (==): Checks if two values are the same.
Not equal to (!=): Checks if two values are different.
Greater than (>): Checks if the left value is larger.
Less than (<): Checks if the left value is smaller.
Greater than or equal to (>=): Checks if the left value is larger or equal.
Less than or equal to (<=): Checks if the left value is smaller or equal.
These operators return a boolean value (true or false) and are essential for controlling the flow of programs using conditional statements like if, while, and for loops.
Operator
Description
Example
==
Equal to
a == b
!=
Not equal to
a != b
>
Greater than
a > b
<
Less than
a < b
>=
Greater than or equal to
a >= b
<=
Less than or equal to
a <= b
For example:
C
C
#include <stdio.h>
int main() { int a = 5, b = 10; printf("%d == %d is %d\n", a, b, a == b); printf("%d != %d is %d\n", a, b, a != b); printf("%d > %d is %d\n", a, b, a > b); printf("%d < %d is %d\n", a, b, a < b); printf("%d >= %d is %d\n", a, b, a >= b); printf("%d <= %d is %d\n", a, b, a <= b); return 0; }
Output:
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job Bootcamp
3. Bitwise Operators
Bitwise operators are used to perform operations at the bit level. Bitwise operators perform operations on the binary representations of integers. They include AND (&), OR (), XOR (^), NOT (~), left shift (<<), and right shift (>>). These operators manipulate individual bits within integer values, enabling lowlevel data processing, such as setting, clearing, and toggling specific bits, commonly used in system programming and performancecritical applications.
Operator
Description
Example
&
Bitwise AND
a & b
`
`
Bitwise OR
^
Bitwise XOR
a ^ b
~
Bitwise NOT
~a
<<
Left shift
a << b
>>
Right shift
a >> b
For example:
C
C
#include <stdio.h>
int main() { int a = 5, b = 3; printf("%d & %d is %d\n", a, b, a & b); printf("%d  %d is %d\n", a, b, a  b); printf("%d ^ %d is %d\n", a, b, a ^ b); printf("~%d is %d\n", a, ~a); printf("%d << %d is %d\n", a, b, a << b); printf("%d >> %d is %d\n", a, b, a >> b); return 0; }
Output:
4. Shift Operators
Shift operators, like << (left shift) and >> (right shift), move bits left or right. x << 2 shifts the bits of x two positions to the left.
For example:
C
C
#include <stdio.h>
int main() { int a = 8; // Binary: 1000
printf("%d << 2 is %d\n", a, a << 2); // Left shift by 2: Binary 100000 printf("%d >> 2 is %d\n", a, a >> 2); // Right shift by 2: Binary 10
return 0; }
Output:
5. Ternary or Conditional Operators
The ternary operator (? :) provides a concise way to express conditional statements. It evaluates a condition and returns one of two values based on its truth.
For example:
C
C
#include <stdio.h>
int main() { int a = 5, b = 3; int max = (a > b) ? a : b;
printf("The maximum is %d\n", max);
return 0; }
Output:
6. Logical Operators
Logical operators, including && (AND),  (OR), and ! (NOT), are used in Boolean expressions. x && y is true if both x and y are true.
Operator
Description
Example
&&
Logical AND
a && b
`
`
!
Logical NOT
!a
For example:
C
C
#include <stdio.h>
int main() { int a = 1, b = 0;
if (a && b) { printf("Both conditions are true.\n"); } else { printf("At least one condition is false.\n"); }
return 0; }
Output:
7. Assignment Operators
Assignment operators, like +=, =, *=, etc., combine assignment with other operations. a += 5 is a shorthand for a = a + 5.
For example:
C
C
#include <stdio.h>
int main() { int a = 5; a += 3; // Equivalent to a = a + 3 printf("The value of a is %d\n", a);
return 0; }
Output:
8. Misc Operators
The sizeof operator returns the size, in bytes, of a variable or data type. The comma operator allows multiple expressions in a single statement, evaluating them from left to right.
For example:
C
C
#include <stdio.h>
int main() { int a = 5; printf("The size of 'a' is %lu bytes\n", sizeof(a)); printf("The address of 'a' is %p\n", (void*)&a);
return 0; }
Output:
Frequently Asked Questions
What is the == operator in C?
The == operator in C is used for equality comparison between two operands.
What is the use of Operators in C programming?
Operators in C programming are used to perform various operations on variables and values, such as arithmetic, logical, relational, and bitwise operations. They facilitate calculations, comparisons, and data manipulation, enabling effective control and functionality within the program.
How many operators can C have?
C has a wide range of operators, including arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional (ternary), and special operators. There are over 45 operators, each serving specific operations and functionalities in programming.
What is the difference between the ‘=’ and ‘==’ operators?
The = operator is an assignment operator used to assign a value to a variable, while the == operator is a relational operator used to compare two values for equality, returning true if they are equal and false otherwise.
Conclusion
Understanding operators in C is foundational for effective programming. They encapsulate basic to complex operations, enabling the development of a wide range of applications. As the C language continues to be relevant across many domains, a strong grasp of operators remains a vital skill for programmers.