C Operators
Arithmetic Operators
| Operator | Name | Example | Result |
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division | 10 / 3 | 3 (integer division) |
% | Modulus (remainder) | 10 % 3 | 1 |
++ | Increment | a++ or ++a | a + 1 |
-- | Decrement | a-- or --a | a - 1 |
Relational Operators
| Operator | Name | Example | Result |
== | Equal to | 5 == 5 | 1 (true) |
!= | Not equal to | 5 != 3 | 1 (true) |
> | Greater than | 5 > 3 | 1 (true) |
< | Less than | 5 < 3 | 0 (false) |
>= | Greater than or equal | 5 >= 5 | 1 (true) |
<= | Less than or equal | 3 <= 5 | 1 (true) |
Logical Operators
| Operator | Name | Example | Result |
&& | Logical AND | (5>3) && (2<4) | 1 (both true) |
|| | Logical OR | (5>3) || (2>4) | 1 (one true) |
! | Logical NOT | !(5>3) | 0 (negates true) |
Bitwise Operators
| Operator | Name | Example (a=5, b=3) | Result |
& | Bitwise AND | 5 & 3 (0101 & 0011) | 1 (0001) |
| | Bitwise OR | 5 | 3 (0101 | 0011) | 7 (0111) |
^ | Bitwise XOR | 5 ^ 3 (0101 ^ 0011) | 6 (0110) |
~ | Bitwise NOT | ~5 | -6 |
<< | Left shift | 5 << 1 | 10 |
>> | Right shift | 5 >> 1 | 2 |
Assignment Operators
| Operator | Equivalent | Example |
= | Assign | a = 5 |
+= | a = a + b | a += 3 |
-= | a = a - b | a -= 3 |
*= | a = a * b | a *= 3 |
/= | a = a / b | a /= 3 |
%= | a = a % b | a %= 3 |
Arithmetic, Relational and Logical Operators
#include <stdio.h>
int main() {
int a = 10, b = 3;
// Arithmetic
printf("a + b = %d\n", a + b); // 13
printf("a - b = %d\n", a - b); // 7
printf("a * b = %d\n", a * b); // 30
printf("a / b = %d\n", a / b); // 3 (integer division)
printf("a %% b = %d\n", a % b); // 1
// Increment / Decrement
int x = 5;
printf("x++: %d\n", x++); // 5 (post-increment: use then increment)
printf("x: %d\n", x); // 6
printf("++x: %d\n", ++x); // 7 (pre-increment: increment then use)
// Relational
printf("\n5 == 5: %d\n", 5 == 5); // 1
printf("5 != 3: %d\n", 5 != 3); // 1
printf("5 > 8: %d\n", 5 > 8); // 0
// Logical
int age = 20;
printf("\nage >= 18 && age <= 60: %d\n", age >= 18 && age <= 60); // 1
printf("age < 18 || age > 60: %d\n", age < 18 || age > 60); // 0
printf("!(age == 20): %d\n", !(age == 20)); // 0
return 0;
}
Bitwise, Ternary and sizeof Operators
#include <stdio.h>
int main() {
int a = 5, b = 3; // a = 0101, b = 0011 in binary
// Bitwise operators
printf("a & b = %d\n", a & b); // 1 (0001)
printf("a | b = %d\n", a | b); // 7 (0111)
printf("a ^ b = %d\n", a ^ b); // 6 (0110)
printf("~a = %d\n", ~a); // -6
printf("a << 1 = %d\n", a << 1); // 10 (multiply by 2)
printf("a >> 1 = %d\n", a >> 1); // 2 (divide by 2)
// Ternary operator: condition ? value_if_true : value_if_false
int num = 7;
char *result = (num % 2 == 0) ? "even" : "odd";
printf("\n%d is %s\n", num, result); // 7 is odd
int max = (a > b) ? a : b;
printf("Max of %d and %d is %d\n", a, b, max); // 5
// sizeof operator
printf("\nsizeof(int): %zu\n", sizeof(int));
printf("sizeof(double): %zu\n", sizeof(double));
printf("sizeof(a): %zu\n", sizeof(a)); // same as sizeof(int)
return 0;
}