These are the notes for C episode 3. You will learn the arithmetic operators, why 250 / 4 is 62 in C, how casting fixes it (and how casting too late does not), what % does, operator precedence, ++, -- and +=, the difference between a++ and ++a, why comparisons give 1 or 0, the = vs == trap, and how && short-circuits to protect you from dividing by zero. Every example below was compiled with gcc -O2 main.c -o main -lm (gcc 13) and the output shown is the real output.
The problem: where did ₹2 go?
Four friends eat chaat and the bill is ₹250. We let C split it:
#include <stdio.h>
int main() {
int bill = 250;
int friends = 4;
printf("Each pays: %d\n", bill / friends);
return 0;
}
Output:
Each pays: 62
But 62 × 4 = 248. The answer should be 62.50, and the chaat bhaiya is still waiting for ₹2.
Maybe it's %d? Switch to %f:
printf("Each pays: %f\n", bill / friends);
Even worse. gcc warns:
main.c:6:25: warning: format '%f' expects argument of type 'double', but argument 2 has type 'int' [-Wformat=]
and the program prints:
Each pays: 0.000000
So the problem is not printf. It is the division itself: bill / friends is still an int.
Integer division: int / int = int
| Expression | Types | Result |
|---|---|---|
250 / 4 |
int / int | 62 (the .50 is chopped off) |
250.0 / 4 |
double / int | 62.5 |
7 / 2 |
int / int | 3 |
In C, an int divided by an int gives an int. The fractional part is thrown away (it is not rounded). This is called integer division. It's like a cashier who only has ₹1 coins: he simply cannot give you paise.
The fix: make at least one side a decimal, and C does decimal division.
Edge case: with negative numbers, C truncates toward zero.
-7 / 2is-3(not-4), and-7 % 2is-1.
The fix: casting with (float)
Remember the containers from episode 2: bill sits in an egg tray (int), where half an egg is impossible. Before dividing, pour it into a measuring jug. In C that is called casting: the type in round brackets, then the value: (float)bill.
#include <stdio.h>
int main() {
int bill = 250;
int friends = 4;
printf("Each pays: %.2f\n", (float)bill / friends);
printf("Each pays: %.2f\n", 250.0 / 4);
printf("Each pays: %.2f\n", (float)(bill / friends));
return 0;
}
Output:
Each pays: 62.50
Each pays: 62.50
Each pays: 62.00
- Line 1:
billbecomes250.0first, so the division is decimal: 62.50. - Line 2:
250.0is already a decimal literal: 62.50. - Line 3: the common mistake. The brackets make
bill / friendsrun first as integer division (62), and only then is62turned into62.0. The 50 paise were already gone. Cast before you divide, not after.
Modulus %: the leftovers
The sweet shop has 125 laddoos to pack in boxes of 12. Integer division is exactly what we want for full boxes (nobody sells half a box), and % (modulus) gives the remainder:
#include <stdio.h>
int main() {
int laddoos = 125;
int perBox = 12;
printf("Full boxes: %d\n", laddoos / perBox);
printf("Left over: %d\n", laddoos % perBox);
return 0;
}
Output:
Full boxes: 10
Left over: 5
125 % 12 is the same as 125 - 10 * 12 = 5, in one operator.
In C, % works only on integers:
#include <stdio.h>
int main() {
float laddoos = 125;
printf("%f\n", laddoos % 12);
return 0;
}
main.c:5:28: error: invalid operands to binary % (have 'float' and 'int')
(For a floating-point remainder there is fmod() in <math.h>, but you will rarely need it.)
All five arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
add | 250 + 25 |
275 |
- |
subtract | 250 - 50 |
200 |
* |
multiply | 62 * 4 |
248 |
/ |
divide | 250 / 4 |
62 (int / int!) |
% |
remainder | 125 % 12 |
5 |
Two famous modulus tricks (they come up in interviews a lot):
n % 2 == 0meansnis even;n % 2is1for a positive odd number.n % 10gives the last digit ofn(2026 % 10is6).
Precedence and brackets: the report card that said 125
Maths 80, Science 90. We want the average:
#include <stdio.h>
int main() {
int maths = 80, science = 90;
printf("Average: %d\n", maths + science / 2);
return 0;
}
Output:
Average: 125
125 out of 100? Remember BODMAS: division happens before addition. C did 90 / 2 = 45 first, then 80 + 45 = 125. This is operator precedence. The fix is brackets, because whatever is in brackets always happens first:
printf("Average: %d\n", (maths + science) / 2);
Output:
Average: 85
| Precedence (high to low) | Operators |
|---|---|
| 1 | ( ) brackets |
| 2 | ++ -- ! (unary), casts like (float) |
| 3 | * / % |
| 4 | + - |
| 5 | < <= > >= |
| 6 | == != |
| 7 | && |
| 8 | || |
| 9 | = += -= *= /= %= |
Operators on the same level, like * and /, run left to right. Rule: whenever in doubt, add brackets; it makes the code easier to read too.
++, -- and += : the gatekeeper's click counter
At a mela gate, the gatekeeper clicks a counter for every visitor. count = count + 1 written a thousand times gets tiring, so C has shortcuts:
| Shortcut | Same as |
|---|---|
count++; |
count = count + 1; |
count--; |
count = count - 1; |
count += 10; |
count = count + 10; |
count -= 3; |
count = count - 3; |
count *= 2; |
count = count * 2; |
count /= 2; |
count = count / 2; |
count %= 5; |
count = count % 5; |
#include <stdio.h>
int main() {
int count = 0;
count++;
count++;
count += 10;
printf("Visitors: %d\n", count);
count--;
printf("Visitors: %d\n", count);
count *= 2;
printf("Visitors: %d\n", count);
return 0;
}
Output:
Visitors: 12
Visitors: 11
Visitors: 22
a++ vs ++a (a classic interview question)
#include <stdio.h>
int main() {
int a = 5;
int b = a++;
printf("a = %d, b = %d\n", a, b);
int c = 5;
int d = ++c;
printf("c = %d, d = %d\n", c, d);
return 0;
}
Output:
a = 6, b = 5
c = 6, d = 6
- Post-increment
a++: use the old value, then add.bgets 5, thenabecomes 6. - Pre-increment
++c: add first, then use.cbecomes 6, anddgets 6. - On a line of their own (
a++;or++a;) both do exactly the same thing.
Comparisons: C answers 1 or 0
Is the voter 18 or older? Is the seat exactly 18? Is it not 18?
| Operator | Meaning |
|---|---|
== |
equal to (two = signs) |
!= |
not equal to |
< > |
less than, greater than |
<= >= |
less or equal, greater or equal |
#include <stdio.h>
int main() {
int age = 20;
printf("age >= 18 : %d\n", age >= 18);
printf("age == 18 : %d\n", age == 18);
printf("age != 18 : %d\n", age != 18);
printf("age < 18 : %d\n", age < 18);
return 0;
}
Output:
age >= 18 : 1
age == 18 : 0
age != 18 : 1
age < 18 : 0
In C, true is 1 and false is 0. Basic C has no separate true/false type, just numbers. (Any non-zero value counts as true in a condition. C99 added bool via <stdbool.h>, but it is still 1 and 0 underneath.)
The = vs == trap
age is 15, and by mistake we write one = instead of two:
#include <stdio.h>
int main() {
int age = 15;
printf("Is 18? %d\n", age = 18);
printf("age is now %d\n", age);
return 0;
}
Output:
Is 18? 18
age is now 18
A single = did not ask a question; it changed the age to 18 and the expression's value is 18. There is no error. Single = stores; double == asks.
Logical operators: &&, || and !
At the cinema:
| Real life | C | Operator |
|---|---|---|
| Adult film: 18+ and a ticket (both needed) | age >= 18 && hasTicket |
&& AND |
| Discount: student or senior (any one is enough) | isStudent || isSenior |
|| OR |
| Not a student (flips yes and no) | !isStudent |
! NOT |
#include <stdio.h>
int main() {
int age = 20, hasTicket = 1;
printf("Can enter: %d\n", age >= 18 && hasTicket);
int isStudent = 0, isSenior = 1;
printf("Discount: %d\n", isStudent || isSenior);
printf("Not student: %d\n", !isStudent);
return 0;
}
Output:
Can enter: 1
Discount: 1
Not student: 1
A |
B |
A && B |
A || B |
!A |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 |
Short-circuit &&: splitting the bill among zero friends
The app asks "is each share over ₹100?", but today nobody came: friends = 0. 250 / 0 is undefined even in maths. In C, integer division by zero gives no compile error: the program crashes while it runs. On Linux you typically see Floating point exception; with gcc -O2, gcc may turn the guaranteed division by zero into a trap and you see Illegal instruction instead. Either way, the program dies.
The fix: check first. && has a special property: if the left side is false, the right side never runs. This is short-circuit evaluation.
#include <stdio.h>
int main() {
int bill = 250, friends = 0;
printf("Costly? %d\n", friends != 0 && bill / friends > 100);
return 0;
}
Output:
Costly? 0
No crash. friends != 0 was false, so C never did the division. Always put the safety check first in &&. (|| short-circuits too: if the left side is true, the right side is skipped.)
Common mistakes (and the real gcc messages)
| Mistake | Code | What happens |
|---|---|---|
| int / int | 250 / 4 |
no error, prints 62. Fix: 250.0 / 4 or (float)bill / friends |
| Casting too late | (float)(bill / friends) |
no error, prints 62.00. Fix: cast before dividing |
%f with an int expression |
printf("%f", bill / friends); |
warning: format '%f' expects argument of type 'double', but argument 2 has type 'int' [-Wformat=], prints 0.000000 |
% on a float |
float laddoos = 125; laddoos % 12 |
error: invalid operands to binary % (have 'float' and 'int') |
| Missing brackets | maths + science / 2 |
no error, prints 125. Fix: (maths + science) / 2 |
= instead of == |
printf("%d", age = 18); |
no error, age is now 18 |
| Dividing by zero | bill / friends with friends = 0 |
compiles, crashes at run time. Fix: friends != 0 && bill / friends > 100 |
Notice how many of these give no compiler message at all. No error does not mean a correct program, so always check your output.
Mini project: chaat bill splitter
Bill ₹250, a 10% tip, four friends, and we pay in ₹20 notes:
#include <stdio.h>
int main() {
int bill = 250;
int tipPercent = 10;
int friends = 4;
float total = bill + bill * tipPercent / 100.0;
printf("Total with tip: %.2f\n", total);
printf("Each pays: %.2f\n", total / friends);
int notes = (int)total / 20;
int change = (int)total % 20;
printf("20-rupee notes: %d, change: %d\n", notes, change);
return 0;
}
Output:
Total with tip: 275.00
Each pays: 68.75
20-rupee notes: 13, change: 15
- We divide by
100.0, not100, so there is no integer division in the tip. total / friendsis float / int, so decimal division: 68.75.- For
%we cast the total toint, because%needs integers. 13 notes of ₹20 is 260, and 15 is left over.
Practice
- Print whether 47 is even or odd using
%. - Print the last digit of 2026 using
% 10. - Convert 135 minutes into hours and minutes.
- Change the mini project to a ₹300 bill, a 15% tip and 5 friends. What does it print?
- The trap:
int a = 3; int b = a++ + ++a;What is the output, and why is it a trap?
Solutions and hints
1. printf("%d\n", 47 % 2); prints 1, so 47 is odd (0 would mean even).
2. printf("%d\n", 2026 % 10); prints 6.
3. int m = 135; printf("%d h %d min\n", m / 60, m % 60); prints 2 h 15 min.
4. Output:
Total with tip: 345.00
Each pays: 69.00
20-rupee notes: 17, change: 5
5. With gcc 13, printf("%d %d\n", a, b); printed 5 8, but those numbers mean nothing. Modifying a twice in one expression without a sequence point is undefined behaviour in C: different compilers (or flags) may give different answers. gcc even warns with -Wall: warning: operation on 'a' may be undefined [-Wsequence-point]. The real answer: never write code like this.
Quick quiz
What will this code print?
printf("%d\n", 7 / 2 * 2);
- A.
7 - B.
6 - C.
7.0 - D.
8
Answer
B: 6. / and * have the same precedence and run left to right. 7 / 2 is 3 (integer division), then 3 * 2 is 6. (Verified: the program prints 6.)
Interview questions
- Why does
250 / 4give62in C? How do you get62.5? - What is the difference between
(float)a / band(float)(a / b)? - Can you use
%with floating-point numbers in C? - What is the result of
-7 / 2and-7 % 2in C? - What is the difference between
a++and++a? What is wrong witha++ + ++a? - What do comparison operators return in C?
- What is short-circuit evaluation, and how can it prevent a crash?
- What happens on
if (x = 5)instead ofif (x == 5)?
Summary
int / int = int:250 / 4is62. Make one side a decimal to keep the fraction.- Cast before dividing:
(float)bill / friends, not(float)(bill / friends). %gives the remainder and works only on integers;n % 2tests even/odd,n % 10gives the last digit.- Precedence:
* / %before+ -, same level runs left to right, brackets first. ++,--,+=and friends are shortcuts;a++uses then adds,++aadds then uses.- Comparisons give
1(true) or0(false);=stores,==compares. &&,||,!combine conditions, and&&skips the right side when the left is false, so put the safety check first.
Next episode: input with scanf. So far we've written the bill into the code; next time the user types it themselves, and a tiny & will give you a lot of trouble.