C Operators
Operators are symbols that perform operations on variables and values. C has a rich set of operators — arithmetic, relational, logical, assignment, bitwise, increment/decrement, and more. Mastering them is essential for writing any C program.
1. Arithmetic Operators
int a = 10, b = 3;
printf("%d\n", a + b); // 13 (addition)
printf("%d\n", a - b); // 7 (subtraction)
printf("%d\n", a * b); // 30 (multiplication)
printf("%d\n", a / b); // 3 (integer division — truncates)
printf("%d\n", a % b); // 1 (modulus — remainder)
In C, 10 / 3 = 3 (integer division), not 3.333. To get decimal result, cast at least one operand: (float)10 / 3 = 3.333. Also, % modulus only works with integers, not floats.
2. Relational Operators
int a = 10, b = 5;
printf("%d\n", a == b); // 0 (false)
printf("%d\n", a != b); // 1 (true)
printf("%d\n", a > b); // 1 (true)
printf("%d\n", a < b); // 0 (false)
printf("%d\n", a >= b); // 1 (true)
printf("%d\n", a <= b); // 0 (false)
In C, comparison returns 1 for true and 0 for false. There is no boolean type in classic C — the stdbool.h header adds it.
3. Logical Operators
int age = 22;
int hasId = 1; // 1 = true
if (age >= 18 && hasId) {
printf("Can enter\n");
}
if (age < 18 || hasId) {
printf("At least one is true\n");
}
if (!hasId) {
printf("No ID\n");
}
Short-circuit behaviour
&& and || short-circuit — if the result is already known, the second condition is NOT evaluated.
int x = 5;
if (x > 10 && x++ > 0) {
// x++ NOT executed because x > 10 is false
}
printf("%d\n", x); // 5 (unchanged)
4. Assignment Operators
int x = 10;
x += 5; // x = x + 5 → 15
x -= 3; // x = x - 3 → 12
x *= 2; // x = x * 2 → 24
x /= 4; // x = x / 4 → 6
x %= 4; // x = x % 4 → 2
printf("%d\n", x);
5. Increment & Decrement Operators
int x = 5;
// Pre-increment: increment first, then use
printf("%d\n", ++x); // 6, x becomes 6
// Post-increment: use first, then increment
int y = 5;
printf("%d\n", y++); // 5 (prints 5, y becomes 6)
printf("%d\n", y); // 6
// Pre-decrement
int a = 5;
printf("%d\n", --a); // 4
// Post-decrement
int b = 5;
printf("%d\n", b--); // 5 (b becomes 4)
printf("%d\n", b); // 4
Rule of thumb: Use pre-increment (++x) when you don't need the old value. Use post-increment (x++) when you need the old value first. For loops, either works — i++ is more conventional.
6. Bitwise Operators
Operate on individual bits. Very useful in embedded systems, flags, and low-level programming.
int a = 5; // binary: 0101
int b = 3; // binary: 0011
printf("%d\n", a & b); // 1 (AND — 0001)
printf("%d\n", a | b); // 7 (OR — 0111)
printf("%d\n", a ^ b); // 6 (XOR — 0110)
printf("%d\n", ~a); // -6 (NOT — inverts bits)
printf("%d\n", a << 1); // 10 (left shift — multiply by 2)
printf("%d\n", a >> 1); // 2 (right shift — divide by 2)
Use cases:
- Left shift << — fast multiply by powers of 2
- Right shift >> — fast divide by powers of 2
- AND & — check if a bit is set (masking)
- OR | — set a bit
- XOR ^ — toggle a bit
7. Ternary Operator
Short-hand for if-else. Syntax: condition ? valueIfTrue : valueIfFalse.
int age = 20;
char* status = (age >= 18) ? "Adult" : "Minor";
printf("%s\n", status); // Adult
// Find max of two numbers
int a = 10, b = 20;
int max = (a > b) ? a : b;
printf("Max: %d\n", max); // 20
8. sizeof Operator
Returns the size (in bytes) of a variable or type.
printf("%lu\n", sizeof(int)); // 4
printf("%lu\n", sizeof(char)); // 1
printf("%lu\n", sizeof(double)); // 8
printf("%lu\n", sizeof(float)); // 4
int arr[10];
printf("%lu\n", sizeof(arr)); // 40 (10 × 4 bytes)
9. Address-of and Dereference Operators
int x = 10;
int *ptr = &x; // & = address-of
printf("%p\n", ptr); // address
printf("%d\n", *ptr); // * = dereference → 10
These will be covered in detail in the Pointers chapter.
10. Comma Operator
Evaluates multiple expressions left to right, returns the last value.
int x = (1, 2, 3);
printf("%d\n", x); // 3
for (int i = 0, j = 10; i < j; i++, j--) {
printf("%d %d\n", i, j);
}
Operator Precedence
From highest to lowest (most common):
()— Parentheses++ -- !— Unary* / %— Multiplicative+ -— Additive<< >>— Shift< > <= >=— Relational== !=— Equality&— Bitwise AND^— Bitwise XOR|— Bitwise OR&&— Logical AND||— Logical OR?:— Ternary= += -=— Assignment,— Comma
Real-World Example — Calculator
#include <stdio.h>
int main() {
int a = 20, b = 6;
printf("Sum: %d\n", a + b);
printf("Difference: %d\n", a - b);
printf("Product: %d\n", a * b);
printf("Quotient: %d\n", a / b);
printf("Remainder: %d\n", a % b);
// Real division
printf("Real quotient: %.2f\n", (float) a / b);
// Ternary
printf("Bigger: %d\n", (a > b) ? a : b);
return 0;
}
Output
Sum: 26
Difference: 14
Product: 120
Quotient: 3
Remainder: 2
Real quotient: 3.33
Bigger: 20
1. Write a C program to swap two numbers using a third variable, then without a third variable (using + and -).
2. Find the largest of 3 numbers using the ternary operator.
3. Check if a number is even or odd using %.
4. Test bitwise AND, OR, XOR on two numbers and print results.
5. Use sizeof to print sizes of all data types.
Quick Revision
- Arithmetic:
+ - * / % - Relational:
== != < > <= >=— returns 1 or 0 - Logical:
&& || !— short-circuits - Assignment:
= += -= *= /= %= - Increment:
++x(pre),x++(post) - Bitwise:
& | ^ ~ << >> - Ternary:
cond ? a : b - sizeof:
sizeof(int) - Address-of:
&, Dereference:* - Integer division truncates — cast to float for decimals
Interview Questions
Q: Difference between ++x and x++?
A: ++x (pre-increment) increments first, then returns the new value. x++ (post-increment) returns the current value first, then increments. Example: if x=5, ++x gives 6 and x becomes 6; x++ gives 5, then x becomes 6.
Q: Difference between && and & in C?
A: && is logical AND — short-circuits (skips second if first is false). & is bitwise AND — always evaluates both sides and operates on individual bits.
Q: What does the modulus operator % do?
A: It returns the remainder of integer division. 10 % 3 = 1. It only works with integer types, not float or double.
Q: Why does 10 / 3 give 3 instead of 3.33 in C?
A: Because both operands are integers, C performs integer division which truncates the decimal. To get 3.33, cast one operand: (float)10 / 3.
Q: What is the ternary operator?
A: A one-line if-else: condition ? valueIfTrue : valueIfFalse. Example: int max = (a > b) ? a : b;.
Q: What is operator precedence?
A: The order in which operators are evaluated. Parentheses have the highest precedence, comma has the lowest. When in doubt, use parentheses to make the intent clear.
Q: Difference between = and ==?
A: = is assignment — assigns a value to a variable. == is comparison — checks if two values are equal and returns 1 or 0. Confusing them is a very common bug.
Want to Master C Programming?
Join our C Programming course in Varanasi — practical classes and career support at our Sarnath centre.