An operator is a symbol that does some work on values. In a + b, the + is the operator and a and b are the operands. C has many operators, but they fall into a few groups, and you will use most of them in your very first programs.
This page goes through each group with a short program and its output, then ends with a simple precedence table. The examples use int and double from data types in C.
Operator groups at a glance
| Group | Operators | Example |
|---|---|---|
| Arithmetic | + - * / % |
a + b |
| Relational | == != > < >= <= |
marks >= 35 |
| Logical | && || ! |
age >= 18 && hasId |
| Assignment | = += -= *= /= %= |
total += m |
| Increment and decrement | ++ -- |
i++ |
| Bitwise | & | ^ ~ << >> |
n & 1 |
| Conditional (ternary) | ? : |
a > b ? a : b |
| Others | sizeof, &, *, , |
sizeof(int) |
Arithmetic operators in C
#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);
printf("17.0 / 5 = %.2f\n", 17.0 / 5);
printf("-17 / 5 = %d, -17 %% 5 = %d\n", -17 / 5, -17 % 5);
return 0;
}
Output:
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
17.0 / 5 = 3.40
-17 / 5 = -3, -17 % 5 = -2
| Operator | Meaning | Result for 17 and 5 |
|---|---|---|
+ |
Addition | 22 |
- |
Subtraction | 12 |
* |
Multiplication | 85 |
/ |
Division | 3 (integer division) |
% |
Remainder (modulus) | 2 |
Points to remember:
- Integer division drops the decimal part.
17 / 5is3. If either value is a decimal, as in17.0 / 5, the result is a decimal. %works only on integers. It is the remainder, son % 2 == 0meansnis even, andn % 10gives the last digit ofn.- With negative numbers, C rounds division towards zero.
-17 / 5is-3, and the remainder takes the sign of the left value, so-17 % 5is-2. - To print a
%sign withprintf, write%%.
Relational operators in C
Relational operators compare two values. The result is 1 for true and 0 for false.
#include <stdio.h>
int main(void) {
int a = 10, b = 20;
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 >= 10: %d\n", a >= 10);
printf("b <= 15: %d\n", b <= 15);
return 0;
}
Output:
a == b : 0
a != b : 1
a > b : 0
a < b : 1
a >= 10: 1
b <= 15: 0
You will use these mostly inside if and loop conditions, such as if (marks >= 35) or while (i < n).
Common mistake: Writing
if (x = 5)instead ofif (x == 5). The first one stores 5 inx, and since 5 is non-zero, the condition is always true. Compile withgcc -Walland GCC warns you about this.
Logical operators in C
Logical operators combine conditions.
| Operator | Name | True when |
|---|---|---|
&& |
AND | Both sides are true |
|| |
OR | At least one side is true |
! |
NOT | The condition is false |
#include <stdio.h>
int main(void) {
int age = 20, marks = 65;
printf("%d\n", age >= 18 && marks >= 60);
printf("%d\n", age < 18 || marks >= 90);
printf("%d\n", !(age >= 18));
return 0;
}
Output:
1
0
0
C treats 0 as false and any non-zero value as true.
Short-circuit evaluation. C stops as soon as the answer is known. In a && b, if a is false, b is never checked. In a || b, if a is true, b is never checked. This lets you write safe conditions such as:
if (count != 0 && total / count > 50) {
printf("Good average\n");
}
If count is 0, the division never runs, so there is no division by zero.
Assignment operators in C
#include <stdio.h>
int main(void) {
int n = 10;
n += 5; printf("After n += 5 : %d\n", n);
n -= 3; printf("After n -= 3 : %d\n", n);
n *= 2; printf("After n *= 2 : %d\n", n);
n /= 5; printf("After n /= 5 : %d\n", n);
n %= 3; printf("After n %%= 3 : %d\n", n);
return 0;
}
Output:
After n += 5 : 15
After n -= 3 : 12
After n *= 2 : 24
After n /= 5 : 4
After n %= 3 : 1
n += 5 is short for n = n + 5. The same pattern holds for the others.
Increment and decrement operators in C
++ adds 1 and -- subtracts 1. Each has two forms, and the difference only matters when you use the result in the same expression.
#include <stdio.h>
int main(void) {
int x, y;
x = 5; y = x++;
printf("y = x++ -> y = %d, x = %d\n", y, x);
x = 5; y = ++x;
printf("y = ++x -> y = %d, x = %d\n", y, x);
x = 5; y = x--;
printf("y = x-- -> y = %d, x = %d\n", y, x);
x = 5; y = --x;
printf("y = --x -> y = %d, x = %d\n", y, x);
return 0;
}
Output:
y = x++ -> y = 5, x = 6
y = ++x -> y = 6, x = 6
y = x-- -> y = 5, x = 4
y = --x -> y = 4, x = 4
| Form | Name | Steps |
|---|---|---|
x++ |
Post-increment | Use the old value, then add 1 |
++x |
Pre-increment | Add 1, then use the new value |
x-- |
Post-decrement | Use the old value, then subtract 1 |
--x |
Pre-decrement | Subtract 1, then use the new value |
Common mistake: Changing the same variable twice in one expression, such as
x = x++ + ++x;orprintf("%d %d", i++, i++);. This is undefined behaviour in C. Different compilers give different answers, so there is no "correct output". Interview questions that ask for it are testing whether you know that.
Bitwise operators in C
Bitwise operators work on the binary digits (bits) of integers. Take a = 12 (binary 1100) and b = 10 (binary 1010).
#include <stdio.h>
int main(void) {
int a = 12, b = 10;
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("a << 1 = %d\n", a << 1);
printf("a >> 2 = %d\n", a >> 2);
return 0;
}
Output:
a & b = 8
a | b = 14
a ^ b = 6
~a = -13
a << 1 = 24
a >> 2 = 3
| Operator | Name | Bits | Result |
|---|---|---|---|
& |
AND | 1100 & 1010 = 1000 |
8 |
| |
OR | 1100 | 1010 = 1110 |
14 |
^ |
XOR | 1100 ^ 1010 = 0110 |
6 |
<< |
Left shift | 1100 becomes 11000 |
24 (same as × 2) |
>> |
Right shift | 1100 becomes 11 |
3 (same as ÷ 4) |
~a flips every bit. With the usual two's complement storage, ~a equals -a - 1, so ~12 is -13. A common use of & is checking even or odd: n & 1 is 1 for odd numbers and 0 for even numbers. To practise converting between decimal and binary, use the number system converter.
Conditional (ternary) operator
The form is condition ? value_if_true : value_if_false.
#include <stdio.h>
int main(void) {
int a = 12, b = 45, n = 7;
int max = (a > b) ? a : b;
printf("Max = %d\n", max);
printf("%d is %s\n", n, (n % 2 == 0) ? "even" : "odd");
return 0;
}
Output:
Max = 45
7 is odd
It is a short form of a simple if else. Use it for choosing between two values, not for long logic.
sizeof and other operators
| Operator | What it does | Example |
|---|---|---|
sizeof |
Size of a type or variable in bytes | sizeof(int) is usually 4 |
& (unary) |
Address of a variable | scanf("%d", &n) |
* (unary) |
Value at an address | *p |
, |
Evaluates left, then right | for (i = 0, j = 9; i < j; i++, j--) |
The & and * pointer operators are explained in what is a pointer in C.
Operator precedence in C (simple table)
Precedence decides which operator runs first when there are no brackets. Associativity decides the order when two operators have the same precedence.
| Priority | Operators | Associativity |
|---|---|---|
| 1 (highest) | () [] x++ x-- |
Left to right |
| 2 | ++x --x ! ~ unary - sizeof |
Right to left |
| 3 | * / % |
Left to right |
| 4 | + - |
Left to right |
| 5 | << >> |
Left to right |
| 6 | < <= > >= |
Left to right |
| 7 | == != |
Left to right |
| 8 | & then ^ then | |
Left to right |
| 9 | && |
Left to right |
| 10 | || |
Left to right |
| 11 | ?: |
Right to left |
| 12 (lowest) | = += -= and other assignments |
Right to left |
#include <stdio.h>
int main(void) {
printf("%d\n", 2 + 3 * 4);
printf("%d\n", (2 + 3) * 4);
printf("%d\n", 10 - 4 - 3);
printf("%d\n", 20 / 4 * 2);
printf("%d\n", 1 + 2 == 3);
printf("%d\n", 5 > 3 && 2 > 4);
return 0;
}
Output:
14
20
3
10
1
0
2 + 3 * 4:*runs first, so 2 + 12 = 14.10 - 4 - 3: same priority, left to right, so (10 - 4) - 3 = 3.20 / 4 * 2: left to right, so (20 / 4) × 2 = 10.1 + 2 == 3:+runs before==, so 3 == 3 gives 1.
Pro tip: You do not need to memorise the whole table. Remember "multiply and divide before add and subtract, comparisons before && and ||, assignment last", and add brackets whenever an expression is not obvious. Brackets cost nothing and make code easier to read.
Quick revision
/on two integers drops the decimal part.%gives the remainder.- Relational and logical operators give 1 (true) or 0 (false).
&&and||stop early once the answer is known.x++uses the old value.++xuses the new value.- Never change the same variable twice in one expression.
- When unsure about precedence, use brackets.
Operators show up in every program you write next: conditions in if, counters in loops and indexes in arrays in C. For a guided path through the rest of the basics, follow C programming step by step.
Frequently Asked Questions
What is the difference between x++ and ++x in C?
What is the difference between = and == in C?
What does the % operator do in C?
Why does 7 / 2 give 3 in C?
Which operator has the highest precedence in C?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

