Operators
Understand arithmetic, relational, logical, and bitwise operators in C with practical examples.
Arithmetic Operators
Arithmetic operators perform mathematical calculations on numeric operands. C supports addition, subtraction, multiplication, division, and the modulus operator which returns the remainder of integer division. Integer division in C truncates the result toward zero, so dividing 7 by 2 yields 3, not 3.5. The increment and decrement operators provide shorthand for adding or subtracting one from a variable.
#include <stdio.h>
int main(void) {
int a = 17, b = 5;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
int c = 10;
printf("c++ = %d\n", c++);
printf("After increment: %d\n", c);
return 0;
}
Relational Operators
Relational operators compare two values and return either 1 for true or 0 for false. These operators include equal to, not equal to, greater than, less than, greater than or equal to, and less than or equal to. They are most commonly used in conditional statements and loop conditions. In C there is no dedicated boolean type before C99, so integers are used where zero is false and any nonzero value is true.
#include <stdio.h>
int main(void) {
int x = 10, y = 20;
printf("x == y: %d\n", x == y);
printf("x != y: %d\n", x != y);
printf("x > y: %d\n", x > y);
printf("x < y: %d\n", x < y);
printf("x >= 10: %d\n", x >= 10);
printf("y <= 15: %d\n", y <= 15);
return 0;
}
Logical Operators
Logical operators combine or negate boolean expressions. The AND operator returns true only when both operands are true, while the OR operator returns true when at least one operand is true. The NOT operator inverts a boolean value. C uses short-circuit evaluation, meaning it stops evaluating as soon as the result is determined, which can be useful for guarding against null pointer dereferences or division by zero.
#include <stdio.h>
int main(void) {
int a = 1, b = 0, c = 1;
printf("a && b: %d\n", a && b);
printf("a || b: %d\n", a || b);
printf("!a: %d\n", !a);
printf("!b: %d\n", !b);
printf("a && c: %d\n", a && c);
printf("(a || b) && c: %d\n", (a || b) && c);
return 0;
}
Bitwise Operators
Bitwise operators manipulate individual bits within integer values. The AND, OR, XOR, and NOT operators work on each bit position independently. Left and right shift operators move all bits in a value by the specified number of positions. Bitwise operations are essential for low-level programming tasks like setting hardware register flags, creating bit masks, and optimizing certain algorithms.
#include <stdio.h>
int main(void) {
unsigned int a = 0b1100;
unsigned int b = 0b1010;
printf("a & b = %u\n", a & b);
printf("a | b = %u\n", a | b);
printf("a ^ b = %u\n", a ^ b);
printf("~a = %u\n", (unsigned char)~a);
printf("a << 2 = %u\n", a << 2);
printf("a >> 1 = %u\n", a >> 1);
/* Using bitmask to check if bit 3 is set */
unsigned int flags = 0b1011;
int bit3Set = (flags >> 3) & 1;
printf("Bit 3 set: %d\n", bit3Set);
return 0;
}