DEV Community

Aryan Kumar Shah
Aryan Kumar Shah

Posted on

5 C Programming Output Traps That Fooled Me in My First Semester


Hi, I'm Aryan Kumar Shah, a first-year BCA student at MAKAUT, West Bengal. This semester is my first real encounter with C programming, and the biggest lesson so far is this: the output is not always what your brain predicts.

Here are five tiny programs that fooled me. Every output below is real — I compiled and ran each one instead of guessing.

1. Integer division quietly throws away the fraction

#include <stdio.h>
int main() {
    printf("%d", 5 / 2);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

My guess: 2.5
Actual output: 2

In C, when both operands are integers, the result is an integer. The fractional part is discarded, not rounded. The same calculation in Python gives 2.5 with / — and Python needs // to behave like C here. If you want the decimal answer in C, at least one side must be a float: 5.0 / 2 gives 2.5.

2. Characters are numbers in disguise

#include <stdio.h>
int main() {
    printf("%d %c", 'A' + 1, 'A' + 1);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output: 66 B

'A' is stored as its ASCII value, 65. Add 1 and you get 66 — which prints as the number 66 with %d, and as the character B with %c. Same value, two faces. This one idea explains a whole family of exam questions.

3. Multiplication answers before addition

#include <stdio.h>
int main() {
    printf("%d", 2 + 3 * 4);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

My first guess: 20 (2 + 3 = 5, times 4)
Actual output: 14

Operator precedence: * runs before +, so it's 2 + 12 = 14. If you want the other answer, you have to ask for it with brackets: (2 + 3) * 4.

4. Post-increment: use first, increase later

#include <stdio.h>
int main() {
    int a = 5;
    int b = a++;
    printf("a=%d b=%d", a, b);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output: a=6 b=5

a++ hands over the old value first, then increments. So b receives 5, and only afterwards does a become 6. Its twin ++a does it in the opposite order — that single difference is a favourite exam trap.

5. 0.1 + 0.2 is not exactly 0.3

#include <stdio.h>
int main() {
    printf("%.20f", 0.1 + 0.2);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output: 0.30000000000000004441

Computers store decimals in binary, and values like 0.1 have no exact binary form — so tiny errors hide below the usual two decimal places. This is why comparing floats with == is risky, and why money is handled in integers (paise/cents) in serious software.

What I'm taking from this

Predicting output before running code — a "dry run" — is the skill underneath all five traps. I now write my guess first, then compile and check. Being wrong on paper is cheap; being wrong in the exam is not.

If you're also in your first semester and one of these fooled you too, tell me which one in the comments — mine was #4.


I'm Aryan Kumar Shah, a BCA student at MAKAUT learning Python, C, Java and JavaScript, and building small developer tools as I go.
Portfolio: https://aryancodes-dev.github.io/ · GitHub: https://github.com/aryancodes-dev · YouTube: @aryancodes_dev

Top comments (0)