What handles operations in C?
Here's a comprehensive list of operators in C, along with sample code and explanations:
1. Arithmetic Operators:
Addition
+
Subtraction
-
Multiplication
*
Division
/
Modulus
%
#include <stdio.h>
int main() {
int a = 10, b = 5, result;
// Addition
result = a + b;
printf("Addition: %d\n", result);
// Subtraction
result = a - b;
printf("Subtraction: %d\n", result);
// Multiplication
result = a * b;
printf("Multiplication: %d\n", result);
// Division
result = a / b;
printf("Division: %d\n", result);
// Modulus
result = a % b;
printf("Modulus: %d\n", result);
return 0;
}
2. Relational Operators:
Equal to
==
Not equal to
!=
Greater than
>
Less than
<
Greater than or equal to
>=
Less than or equal to
<=
#include <stdio.h>
int main() {
int a = 5, b = 10;
// Equal to
if (a == b)
printf("Equal\n");
else
printf("Not Equal\n");
// Not equal to
if (a != b)
printf("Not Equal\n");
else
printf("Equal\n");
// Greater than
if (a > b)
printf("a is Greater\n");
else
printf("b is Greater\n");
// Less than
if (a < b)
printf("a is Less\n");
else
printf("b is Less\n");
// Greater than or equal to
if (a >= b)
printf("a is Greater or Equal\n");
else
printf("b is Greater\n");
// Less than or equal to
if (a <= b)
printf("a is Less or Equal\n");
else
printf("b is Less\n");
return 0;
}
3. Logical Operators:
Logical AND
&&
Logical OR
||
Logical NOT
!
#include <stdio.h>
int main() {
int a = 1, b = 0;
// Logical AND
if (a && b)
printf("Both are True\n");
else
printf("At least one is False\n");
// Logical OR
if (a || b)
printf("At least one is True\n");
else
printf("Both are False\n");
// Logical NOT
if (!a)
printf("a is False\n");
else
printf("a is True\n");
return 0;
}
4. Assignment Operators:
Assignment
=
Add and assign
+=
Subtract and assign
-=
Multiply and assign
*=
Divide and assign
/=
Modulus and assign
%=
#include <stdio.h>
int main() {
int a = 5, b = 2;
// Assignment
int c = a;
// Add and assign
c += b; // equivalent to c = c + b;
// Subtract and assign
c -= b; // equivalent to c = c - b;
// Multiply and assign
c *= b; // equivalent to c = c * b;
// Divide and assign
c /= b; // equivalent to c = c / b;
// Modulus and assign
c %= b; // equivalent to c = c % b;
return 0;
}
5. Increment and Decrement Operators:
Increment
++
Decrement
--
#include <stdio.h>
int main() {
int a = 5;
// Increment
a++; // equivalent to a = a + 1;
// Decrement
a--; // equivalent to a = a - 1;
return 0;
}
6. Bitwise Operators:
Bitwise AND
&
Bitwise OR
|
Bitwise XOR
^
Bitwise NOT
~
Left shift
<<
Right shift
>>
#include <stdio.h>
int main() {
int a = 5, b = 3, result;
// Bitwise AND
result = a & b;
printf("Bitwise AND: %d\n", result);
// Bitwise OR
result = a | b;
printf("Bitwise OR: %d\n", result);
// Bitwise XOR
result = a ^ b;
printf("Bitwise XOR: %d\n", result);
// Bitwise NOT
result = ~a;
printf("Bitwise NOT: %d\n", result);
// Left shift
result = a << 1; // equivalent to a * 2^1
printf("Left shift: %d\n", result);
// Right shift
result = a >> 1; // equivalent to a / 2^1
printf("Right shift: %d\n", result);
return 0;
}
7. Conditional Operator (Ternary Operator):
? :
#include <stdio.h>
int main() {
int a = 5, b = 10, max;
// Conditional Operator
max = (a > b) ? a : b;
printf("Maximum: %d\n", max);
return 0;
}
8. sizeof Operator:
sizeof
#include <stdio.h>
int main() {
int a;
float b;
char c;
printf("Size of int: %lu bytes\n", sizeof(a));
printf("Size of float: %lu bytes\n", sizeof(b));
printf("Size of char: %lu bytes\n", sizeof(c));
return 0;
}
9. Comma Operator:
,
#include <stdio.h>
int main() {
int a = 5, b = 10, c;
// Comma Operator
c = (a++, b++, a + b);
printf("Result: %d\n", c);
return 0;
}
These are the basic operators in C. Understanding and mastering these operators is crucial for writing efficient and effective C programs.
Subscribe to my newsletter
Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jyotiprakash Mishra
Jyotiprakash Mishra
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.