Skills101

C Programming · Lesson 10 of 10 · Updated ·

C Output Questions — Tricky "Predict the Output" Problems Explained

Placement tests love short C programs that ask "what is the output?" They test whether you really understand the rules. Every program below was compiled and run; the output shown is real.

Arrays and pointer arithmetic: element i of array a is exactly *(a + i) — the key to many output questions

Method: go line by line, keep a small table of every variable's value, and remember the traps: integer division, ++ before/after, short-circuit, switch fall-through, macro expansion, and sizeof vs strlen.

1. Integer division and promotion

#include <stdio.h>
int main(void) {
    int a = 7, b = 2;
    float c = a / b;
    float d = (float)a / b;
    printf("%.1f %.1f %d\n", c, d, a % b * 3);
    return 0;
}
3.0 3.5 3

a / b is done in integers (3) before being stored in the float.

2. Short-circuit evaluation

#include <stdio.h>
int main(void) {
    int i = 0, j = 0;
    if (i++ || j++) printf("in ");
    printf("%d %d\n", i, j);
    if (++i || ++j) printf("in ");
    printf("%d %d\n", i, j);
    return 0;
}
1 1
in 2 1

First if: i++ gives 0 (false), so j++ runs too → both 1, no "in". Second: ++i gives 2 (true), so ++j is skipped.

3. Comma operator and assignment in conditions

#include <stdio.h>
int main(void) {
    int x = (1, 2, 3);
    int y = 0;
    if (y = 5) printf("assigned ");
    printf("%d %d\n", x, y);
    return 0;
}
assigned 3 5

The comma operator returns its last value. y = 5 is an assignment with value 5 → true.

4. Loops

#include <stdio.h>
int main(void) {
    int i, count = 0;
    for (i = 10; i > 0; i -= 3) count++;
    printf("%d %d\n", count, i);
    int k = 5;
    while (k--) ;
    printf("%d\n", k);
    return 0;
}
4 -2
-1

i goes 10, 7, 4, 1 (4 rounds), then −2 fails the test. while (k--) checks 5, 4, 3, 2, 1, 0 — the last check sees 0 (stop) but still decrements to −1.

5. Pointers and arrays

#include <stdio.h>
int main(void) {
    int a[] = {10, 20, 30, 40};
    int *p = a + 1;
    printf("%d %d %d\n", *p, *(p + 2), p[-1]);
    printf("%d\n", *p++ + 1);
    printf("%d\n", *p);
    return 0;
}
20 40 10
21
30

*p++ reads *p (20), then moves p. So 20 + 1 = 21, and p now points to 30.

6. Strings and sizeof

#include <stdio.h>
#include <string.h>
int main(void) {
    char s[10] = "code";
    char *t = "code";
    printf("%zu %zu %zu\n", sizeof(s), strlen(s), strlen(t));
    printf("%c %s\n", *(s + 1), s + 2);
    return 0;
}
10 4 4
o de

sizeof(s) is the array size (10), not the text length. s + 2 is the string starting at index 2.

7. Static variables

#include <stdio.h>
int counter(void) {
    static int c = 0;
    c += 2;
    return c;
}
int main(void) {
    int total = 0;
    for (int i = 0; i < 3; i++) total += counter();
    printf("%d\n", total);
    return 0;
}
12

counter returns 2, 4, 6 → total 12.

8. Macros

#include <stdio.h>
#define MUL(a, b) a * b
int main(void) {
    printf("%d %d\n", MUL(2, 3), MUL(1 + 2, 3 + 4));
    return 0;
}
6 11

MUL(1 + 2, 3 + 4) → 1 + 2 * 3 + 4 = 1 + 6 + 4 = 11.

9. Character arithmetic

#include <stdio.h>
int main(void) {
    char c = 'A';
    c += 2;
    printf("%c %d %c\n", c, c, 'a' + 25);
    return 0;
}
C 67 z

10. Recursion

#include <stdio.h>
void f(int n) {
    if (n == 0) return;
    f(n / 2);
    printf("%d", n % 2);
}
int main(void) {
    f(13);
    printf("\n");
    return 0;
}
1101

This prints the binary of 13: the recursive call happens before printing, so the digits come out in the right order.

Common mistakes in these questions

  • Forgetting integer division happens before assignment to a float.
  • Missing short-circuit (|| stops at the first true).
  • Mixing up *p++ (value, then move) and (*p)++ (increase the value).
  • Expanding macros without adding brackets mentally.

Quick recap

  • Trace line by line with a variable table.
  • Watch: integer division, pre/post ++, short-circuit, comma operator, = in if, macro expansion, sizeof vs strlen, static.
  • Practise more on our daily question and placement mocks.

Practice what you learned

📝 Quiz: C Output Questions5 questions · instant answers & explanations · +10 XP · counts towards your free certificateTake the quiz →