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

Source: https://skills101.in/learn/c-programming/c-output-questions — Skills101 free tutorial (C Programming). Updated 2026-11-09.

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](https://skills101.in/learn-img/c-programming/array-pointer.svg)

> **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

```c
#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;
}
```

```text
3.0 3.5 3
```

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

## 2. Short-circuit evaluation

```c
#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;
}
```

```text
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

```c
#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;
}
```

```text
assigned 3 5
```

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

## 4. Loops

```c
#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;
}
```

```text
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

```c
#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;
}
```

```text
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

```c
#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;
}
```

```text
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

```c
#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;
}
```

```text
12
```

counter returns 2, 4, 6 → total 12.

## 8. Macros

```c
#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;
}
```

```text
6 11
```

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

## 9. Character arithmetic

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

```text
C 67 z
```

## 10. Recursion

```c
#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;
}
```

```text
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](https://skills101.in/daily) and [placement mocks](https://skills101.in/placement).


Practice quiz and free certificate: https://skills101.in/learn/c-programming/c-output-questions