Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Jul 12, 2024
Easy

Operators in C Programming

Author Nikunj Goel
0 upvote
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 decision-making and problem-solving in programming. 

Operators in C Programming

Understanding operators is essential for anyone looking to delve into the world of C programming.

Types of Operators in C are mentioned below: 

  1. Arithmetic Operators
  2. Relational Operators
  3. Bitwise Operators
  4. Shift Operators
  5. Ternary or Conditional Operators
  6. Logical Operators
  7. Assignment Operators
  8. 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.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / 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:

output

2. Relational Operators

 Relational operators compare two values or expressions:

  1. Equal to (==): Checks if two values are the same.
  2. Not equal to (!=): Checks if two values are different.
  3. Greater than (>): Checks if the left value is larger.
  4. Less than (<): Checks if the left value is smaller.
  5. Greater than or equal to (>=): Checks if the left value is larger or equal.
  6. 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.

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= 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:

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 low-level data processing, such as setting, clearing, and toggling specific bits, commonly used in system programming and performance-critical applications.

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left shifta << b
>>Right shifta >> 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:

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:

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:

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.

OperatorDescriptionExample
&&Logical ANDa && 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:

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:

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:

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.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Arithmetic Operators in C
Next article
Ternary Operator in C