Operators are the symbols that do the work in C: + adds, > compares, && combines conditions and = stores a value. Most behave just as they do in school maths, but a few have surprises, especially integer division and the bitwise operators. This lesson covers every group with a program you can run.
In this lesson you will learn
- The main groups of C operators and what each one is for
- How integer division and the remainder operator really behave
- How
&&and||skip work (short-circuit evaluation) - How bitwise operators change individual bits
- The difference between
i++and++i
What Operators Do
An operator takes one, two or three values, called operands, and produces a result. In price * qty, the operator is * and the operands are price and qty.
- Unary operators take one operand:
-x,!done,count++. - Binary operators take two:
a + b,x < y. - The ternary operator
?:takes three.
When an expression mixes several operators, precedence decides which runs first. That is covered in detail in operator precedence and associativity.
Arithmetic, Relational, Logical and Bitwise Operators
Arithmetic: + – * / %
#include <stdio.h>
int main(void) {
int apples = 17, kids = 5;
printf("each kid gets %d apples\n", apples / kids); /* integer division */
printf("apples left over: %d\n", apples % kids); /* remainder */
printf("exact share: %.2f\n", (double)apples / kids); /* cast first */
printf("-17 / 5 = %d, -17 %% 5 = %d\n", -17 / 5, -17 % 5);
return 0;
}
Output
each kid gets 3 apples
apples left over: 2
exact share: 3.40
-17 / 5 = -3, -17 % 5 = -2
- When both operands are integers,
/throws away the fractional part: 17 / 5 is 3, not 3.4. %gives the remainder and works only on integers.- To get 3.4, convert one operand to
doublebefore dividing. - Since C99, division rounds toward zero, so -17 / 5 is -3 and the remainder takes the sign of the left operand: -2.
Relational and Logical: == != < > <= >= && || !
Comparisons produce 1 for true and 0 for false. Logical operators combine those results.
#include <stdio.h>
static int checks = 0;
static int is_positive(int x) { checks++; return x > 0; }
int main(void) {
int age = 20, has_ticket = 1;
printf("can enter: %d\n", age >= 18 && has_ticket);
printf("child or senior: %d\n", age < 12 || age >= 65);
printf("not adult: %d\n", !(age >= 18));
/* && stops as soon as the answer is known (short-circuit) */
int r = is_positive(-3) && is_positive(5);
printf("result %d after %d function call(s)\n", r, checks);
return 0;
}
Output
can enter: 1
child or senior: 0
not adult: 0
result 0 after 1 function call(s)
The last line shows short-circuit evaluation. Because the left side of && was false, the answer had to be false, so C never called the right-hand function. || does the same when its left side is true. This makes checks like if (p != NULL && p->age > 18) safe.
Bitwise: & | ^ ~ << >>
Bitwise operators work on the individual 0s and 1s inside a number. They are used for flags, hardware registers, compression and fast arithmetic tricks.
#include <stdio.h>
static void bits(unsigned v) { /* print the low 8 bits */
for (int i = 7; i >= 0; i--) putchar((v >> i) & 1u ? '1' : '0');
}
int main(void) {
unsigned a = 12, b = 10; /* 00001100 and 00001010 */
printf("a = "); bits(a); printf(" (%u)\n", a);
printf("b = "); bits(b); printf(" (%u)\n", b);
printf("a & b = "); bits(a & b); printf(" (%u)\n", a & b);
printf("a | b = "); bits(a | b); printf(" (%u)\n", a | b);
printf("a ^ b = "); bits(a ^ b); printf(" (%u)\n", a ^ b);
printf("a << 1 = "); bits(a << 1); printf(" (%u)\n", a << 1);
printf("a >> 2 = "); bits(a >> 2); printf(" (%u)\n", a >> 2);
return 0;
}
Output
a = 00001100 (12)
b = 00001010 (10)
a & b = 00001000 (8)
a | b = 00001110 (14)
a ^ b = 00000110 (6)
a << 1 = 00011000 (24)
a >> 2 = 00000011 (3)
&keeps a bit only where both numbers have a 1.|keeps a bit where either number has a 1.^(XOR) keeps a bit where exactly one number has a 1.<< 1shifts every bit left, which doubles the value;>> 2shifts right twice, dividing by 4.~flips every bit.
Use unsigned types for bit work. Shifting negative numbers, or shifting by as many bits as the type is wide, is not well defined.
Operators in Working Code
This program covers the remaining everyday operators: compound assignment, increment, the conditional operator and sizeof.
#include <stdio.h>
int main(void) {
int score = 10;
score += 5; /* score = score + 5 */
score *= 2; /* score = score * 2 */
printf("score = %d\n", score);
int i = 5;
int a = i++; /* use, then increase */
int b = ++i; /* increase, then use */
printf("a = %d, b = %d, i = %d\n", a, b, i);
int marks = 47;
printf("result: %s\n", marks >= 40 ? "pass" : "fail");
printf("sizeof(int) = %zu, sizeof(double) = %zu\n", sizeof(int), sizeof(double));
return 0;
}
Output
score = 30
a = 5, b = 7, i = 7
result: pass
sizeof(int) = 4, sizeof(double) = 8
score += 5is shorthand forscore = score + 5. Every arithmetic and bitwise operator has a compound form:-=,*=,/=,%=,&=,|=,^=,<<=,>>=.i++gives the old value and then adds 1;++iadds 1 first and gives the new value. Soagot 5, thenibecame 6, then++imade it 7 andbgot 7.condition ? x : ypicksxif the condition is true, otherwisey. It is a compact if-else that produces a value.sizeofgives the size in bytes of a type or variable. The exact numbers depend on the system; 4 and 8 are typical.
Integer Division and Other Traps
- Integer division.
double avg = total / count;divides two integers first and only then converts. Write(double)total / count. =instead of==.if (x = 0)assigns 0 and is always false. Compile with-Wallto get a warning.- Bitwise vs logical.
&and&&are different.2 & 1is 0, but2 && 1is 1. - Precedence of bitwise operators.
x & 1 == 0meansx & (1 == 0). Add brackets:(x & 1) == 0. - Changing a variable twice in one expression.
i = i++ + 1;is undefined behaviour. Use separate statements. - Dividing by zero. Integer division or
%by zero is undefined and usually crashes; check the divisor first. See handling divide by zero.
C Operators Cheat Sheet
| Group | Operators | Example | Result |
|---|---|---|---|
| Arithmetic | + - * / % | 17 % 5 | 2 |
| Relational | == != < > <= >= | 3 >= 3 | 1 |
| Logical | && || ! | !(5 > 2) | 0 |
| Bitwise | & | ^ ~ << >> | 12 ^ 10 | 6 |
| Assignment | = += -= *= /= %= … | x *= 3 | x = x * 3 |
| Increment / decrement | ++ -- | i++ | old value, then i + 1 |
| Conditional | ?: | a > b ? a : b | the larger value |
| Other | sizeof , & * . -> (type) | sizeof(char) | 1 |
Practice Exercises
Exercise 1. What do 29 / 4 and 29 % 4 give?
Show answer
7 and 1, because 4 × 7 = 28 and 29 − 28 = 1.
Exercise 2. Using %, write a condition that is true when year is a leap year.
Show answer
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
Exercise 3. After int x = 3; int y = x++ * 2;, what are x and y?
Show answer
y is 6 (the old value 3 was used) and x is 4.
Frequently Asked Questions
What is the difference between = and ==?
= stores a value in a variable. == compares two values and gives 1 or 0.
Does C have a power operator?
No. ^ is bitwise XOR, not a power. Use pow() from <math.h> for real numbers, or a loop for whole numbers.
Can I use % with floating-point numbers?
No. Use fmod() from <math.h> instead.
Is i++ faster than ++i?
For plain integers, modern compilers produce the same code. Choose the one whose meaning you need.
Summary
- Arithmetic operators work as expected, but integer division drops the fraction.
- Comparisons and logical operators produce 1 or 0;
&&and||short-circuit. - Bitwise operators work on individual bits; use unsigned types for them.
i++uses then increments;++iincrements then uses.- Add brackets whenever you mix groups of operators.
