Predict the Output: C Questions
Written tests for placements love one question type: "What is the output of this program?" They look tiny, but each one hides a rule of C: integer division, character codes, operator precedence, switch fall-through, unsigned conversion and more. If you know the rule, the answer takes ten seconds.
This page has 15 output questions. Every program was compiled and run, and every answer is explained with the rule behind it. Cover the output, predict it, then check. We deliberately skip questions like i = i++ + ++i: those have undefined behavior, so there is no correct answer, and saying so is the right reply in an interview.
What You Will Learn
- A method to solve output questions step by step.
- Traps with integer division, char arithmetic and octal numbers.
- switch fall-through, the comma operator and short-circuit evaluation.
- Why sizeof doesn't evaluate its operand.
- Unsigned/signed and floating-point comparison surprises.
- How to spot undefined behavior in a question.
Predict the Output: C Questions Explained
Method: (1) Find the types of every value (int, char, unsigned, float). (2) Apply precedence and associativity. (3) Trace variables line by line in a small table. (4) Check the format specifier in printf. (5) If a variable is changed twice in one expression, or memory is misused, answer "undefined behavior".
Memory trick: Output questions are riddles with a hidden rule. Before calculating, ask "what is the trick here?" Nine times out of ten it is one of: int division, char = number, missing break, unsigned, float precision, short-circuit.
Questions 1–5: Numbers and Characters
Q1. Integer division
#include <stdio.h>
int main(void)
{
int a = 7, b = 2;
float f = a / b;
printf("%d %.1f %.1f\n", a / b, f, (float) a / b);
return 0;
}
3 3.0 3.5
Rule: int / int is int division, so a / b is 3 before it is stored in f. Only casting an operand first gives 3.5.
Q2. Characters are numbers
#include <stdio.h>
int main(void)
{
char c = 'A';
printf("%c %d %c %d\n", c + 2, c + 2, 'a' - 32, '9' - '0');
return 0;
}
C 67 A 9
Rule: a char is stored as its ASCII code: 'A' = 65, so 'A' + 2 = 67 = 'C'. Lower and upper case differ by 32. '9' - '0' converts a digit character to its value.
Q3. Octal literal
#include <stdio.h>
int main(void)
{
int x = 010, y = 0x10;
printf("%d %d\n", x, y);
return 0;
}
8 16
Rule: a leading 0 means octal (010 = 8) and 0x means hexadecimal (0x10 = 16).
Q4. Modulus with negative numbers
#include <stdio.h>
int main(void)
{
printf("%d %d %d %d\n", 7 % 3, -7 % 3, 7 % -3, -7 / 2);
return 0;
}
1 -1 1 -3
Rule: since C99, division truncates toward zero (−7 / 2 = −3), and the remainder has the sign of the dividend (left operand).
Q5. Pre and post increment in separate statements
#include <stdio.h>
int main(void)
{
int i = 5, a, b;
a = i++;
b = ++i;
printf("%d %d %d\n", a, b, i);
return 0;
}
5 7 7
Rule: i++ gives the old value (5) then i becomes 6; ++i makes i 7 then gives 7. Each statement changes i only once, so this is well defined.
Questions 6–10: Operators and Control Flow
Q6. switch without break
#include <stdio.h>
int main(void)
{
int n = 2;
switch (n)
{
case 1: printf("one ");
case 2: printf("two ");
case 3: printf("three ");
default: printf("other");
}
printf("\n");
return 0;
}
two three other
Rule: execution jumps to case 2 and falls through every following case until a break or the end.
Q7. sizeof doesn't evaluate
#include <stdio.h>
int main(void)
{
int i = 10;
size_t s = sizeof(i++);
printf("%zu %d\n", s, i);
return 0;
}
4 10
Rule: sizeof only needs the type of its operand, which is known at compile time, so i++ never runs. (4 assumes a 4-byte int.)
Q8. Comma operator
#include <stdio.h>
int main(void)
{
int x, y;
x = (1, 2, 3);
y = 1, 2, 3;
printf("%d %d\n", x, y);
return 0;
}
3 1
Rule: the comma operator evaluates left to right and gives the last value, so x = 3. Without brackets, = has higher precedence than ,, so y = 1 happens first.
Q9. Short-circuit evaluation
#include <stdio.h>
int main(void)
{
int a = 0, b = 5, r;
r = (a != 0) && (++b > 5);
printf("%d %d\n", r, b);
r = (a == 0) || (++b > 5);
printf("%d %d\n", r, b);
return 0;
}
0 5 1 5
Rule: && stops if the left side is false, and || stops if the left side is true. In both lines the right side, including ++b, never runs.
Q10. The extra semicolon
#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i < 3; i++);
{
printf("i = %d\n", i);
}
return 0;
}
i = 3
Rule: the ; after the for header is an empty loop body. The loop runs 3 times doing nothing, then the block runs once with i = 3.
Questions 11–15: Types, Functions and Traps
Q11. Unsigned comparison
#include <stdio.h>
int main(void)
{
unsigned int u = 1;
int s = -1;
if (s < u)
printf("-1 is less\n");
else
printf("-1 is NOT less\n");
return 0;
}
-1 is NOT less
Rule: when int meets unsigned int, the int is converted to unsigned. −1 becomes a huge number (4294967295 with 32-bit int), which is bigger than 1.
Q12. Float comparison
#include <stdio.h>
int main(void)
{
float f = 0.1f;
if (f == 0.1)
printf("equal\n");
else
printf("not equal\n");
return 0;
}
not equal
Rule: 0.1 can't be stored exactly. 0.1f (float) and 0.1 (double) are two different approximations. Compare floats with a small tolerance instead of ==.
Q13. printf returns a value
#include <stdio.h>
int main(void)
{
int n = printf("Hello\n");
printf("%d\n", n);
return 0;
}
Hello 6
Rule: printf returns the number of characters printed: 5 letters plus the newline = 6.
Q14. static variable in a function
#include <stdio.h>
int next(void)
{
static int count = 0;
int normal = 0;
count++;
normal++;
return count * 10 + normal;
}
int main(void)
{
int a = next();
int b = next();
int c = next();
printf("%d %d %d\n", a, b, c);
return 0;
}
11 21 31
Rule: the static variable is created once and keeps counting (1, 2, 3); the normal local starts at 0 on every call, so it is always 1. (Calling next() three times in one printf would leave the order unspecified, so the calls are in separate statements.)
Q15. Nested ternary and assignment in a condition
#include <stdio.h>
int main(void)
{
int x = 15, y = 0;
char *size = x > 20 ? "big" : x > 10 ? "medium" : "small";
if (y = 5)
printf("%s, y became %d\n", size, y);
return 0;
}
medium, y became 5
Rule: the conditional operator groups right to left, so the second ?: is tried when x > 20 is false. if (y = 5) assigns 5 (non-zero, so true); this is a classic bug when == was intended.
Important Points to Remember
- int / int = int; cast before dividing to get decimals.
- char is a number (ASCII); 'A' = 65, 'a' = 97, '0' = 48.
- 010 is octal (8); 0x10 is hex (16).
- switch falls through without break.
- sizeof doesn't evaluate its operand; && and || short-circuit.
- Signed vs unsigned comparison converts to unsigned; never compare floats with ==.
Common Mistakes Beginners Make
| Trap | Wrong guess | Correct idea |
|---|---|---|
float f = 7 / 2; | 3.5 | 3.0: division happens in int first |
int x = 010; | 10 | 8 (octal) |
sizeof(i++) | i increases | i is unchanged |
-1 < 1u | true | false (−1 becomes a huge unsigned value) |
i = i++ + ++i; | Some number | Undefined behavior: no correct answer |
Output Questions Practice
- What is printed by
printf("%d", 5 / 2 * 2);? - What is printed by
printf("%c", 'a' + 1);? - What is the value of
(3, 4, 5) + 1? - What is printed by
int a = 0; if (a++ || a) printf("yes"); else printf("no");? - What does
printf("%d", printf("abc"));print in total?
Show answers
- 4 (5 / 2 = 2, then 2 * 2).
- b.
- 6 (the comma operator gives 5).
- yes:
a++gives 0 (false) and makes a = 1; the right sideais then 1 (true). The||operator has a sequence point after its left operand, so this is well defined. - abc3: the inner printf prints "abc" and returns 3, which the outer printf prints.
Coding Exercise
Predict, then run: what does this program print? Explain each value.
#include <stdio.h>
int main(void)
{
int a = 10, b = 3;
double d = a / b;
char ch = 'C';
printf("%.2f %d %d %c\n", d, a % b, ch - 'A', ch + 1);
printf("%d\n", a > b ? a - b : b - a);
return 0;
}
Show answer
3.00 1 2 D 7
a / b is int division (3), stored as 3.00. 10 % 3 = 1. 'C' - 'A' = 2. 'C' + 1 printed with %c is 'D'. 10 > 3, so the ternary gives 10 − 3 = 7.
Quick Quiz: Output Questions (MCQ)
-
printf("%d", 9 / 2);prints:A) 4.5 B) 4 C) 5 D) 4.0
-
int x = 012;stores:A) 12 B) 10 C) 18 D) Error
-
After
int i = 3; sizeof(++i);, i is:A) 3 B) 4 C) 0 D) Undefined
-
printf("%d", -9 % 4);prints:A) 1 B) −1 C) 3 D) −3
-
The output of
i = i++ + 1;is:A) i + 1 B) i + 2 C) i D) Undefined behavior
Show answers
- B: 4.
- B: 10 (octal 12 = 8 + 2).
- A: 3 (sizeof doesn't evaluate).
- B: −1.
- D: undefined behavior.
Quick Revision for Exams and Interviews
Read this before your test. The highlighted words are the answer.
Must-remember facts
| int / int | Truncated int result |
|---|---|
| Leading 0 / 0x | Octal / hex |
| % sign | Follows the left operand |
| switch | Falls through without break |
| sizeof(expr) | Not evaluated |
| && / || | Short-circuit |
| int vs unsigned | Converted to unsigned |
Frequently asked questions
- How do I solve C output questions quickly?
- Find the types, apply precedence, trace variables line by line, and check for undefined behavior.
- What is the output of i = i++ + ++i?
- There is no fixed output: it modifies i twice without a sequence point, so it is undefined behavior.
- Why is 0.1f == 0.1 false?
- Float and double store different approximations of 0.1; compare with a tolerance.
Remember
- Find the hidden rule first, then calculate.
- Double change in one expression = undefined.
Next Topic
Next lesson: C Programming Viva Questions. You will revise the short viva questions examiners ask in practical exams.