Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Output Questions - Part 1
C

Output Questions - Part 1

Challenge yourself with 15 C predict-the-output questions covering sizeof, division, pointers, switch statements, and common traps.

1. What does this sizeof program print?

#include <stdio.h>

int main(void) {
    printf("%zu\n", sizeof(char));
    printf("%zu\n", sizeof(int));
    printf("%zu\n", sizeof(double));
    return 0;
}

Output:

1
4
8

sizeof returns the size of a type in bytes. sizeof(char) is always 1 by the C standard — a char is one byte by definition. int and double are implementation-defined sizes; on this platform they are 4 and 8 bytes. (On 64-bit Linux with the standard ABI, this is the typical answer.)

2. What does this sizeof on a string literal print?

#include <stdio.h>

int main(void) {
    printf("%zu\n", sizeof("hello"));
    return 0;
}

Output:

6

"hello" is an array of char with 6 elements: 'h','e','l','l','o','\0'. The compiler appends the null terminator, so sizeof("hello") is 6, not 5. Counting the visible letters is the #1 trap here — the string is one longer than its printed length.

3. What does this sizeof on a character constant print?

#include <stdio.h>

int main(void) {
    printf("%zu\n", sizeof('a'));
    return 0;
}

Output:

4

In C, a character constant like 'a' has type int, not char — so sizeof('a') is sizeof(int), which is 4 on this platform. (In C++ this is different: sizeof('a') is 1 because char literals are char.) This is a classic C-vs-C++ interview question.

4. What does this integer division print?

#include <stdio.h>

int main(void) {
    printf("%d\n", 7 / 2);
    printf("%d\n", -7 / 2);
    return 0;
}

Output:

3
-3

In C, integer division truncates toward zero (since C99). 7 / 2 is 3 and -7 / 2 is -3 — the fraction is simply dropped, not rounded down. This differs from Python’s floor division, where -7 // 2 is -4. Truncation toward zero is the C behavior interviewers test.

5. What does this modulo program print?

#include <stdio.h>

int main(void) {
    printf("%d\n", 7 % 3);
    printf("%d\n", -7 % 3);
    printf("%d\n", 7 % -3);
    return 0;
}

Output:

1
-1
1

In C99, the result of % has the sign of the dividend (the left operand). So -7 % 3 is -1 and 7 % -3 is 1. This is the opposite convention from Python, where the result takes the sign of the divisor. Combined with truncating division, the identity a == (a / b) * b + (a % b) always holds.

6. What does this short-circuit program print?

#include <stdio.h>

int main(void) {
    int a = 0, b = 1, c = 1;

    printf("%d\n", a && b);
    printf("%d\n", a || b);
    printf("%d\n", a || (c = 0));
    printf("%d\n", c);
    return 0;
}

Output:

0
1
0
0

&& and || are short-circuit operators. a && b stops at the first false operand (a = 0), giving 0. a || b stops at the first true operand (b = 1), giving 1. In a || (c = 0), a is 0 (false), so || must evaluate the right side — c = 0 runs and the expression is 0. The final printf confirms c is now 0. The trap: short-circuit means the right side is skipped only when the left side already decides the result.

7. What does this comma operator print?

#include <stdio.h>

int main(void) {
    int a = 1, b = 2, c = 3;
    int x = (a, b, c);
    printf("%d\n", x);
    return 0;
}

Output:

3

The comma operator evaluates each expression left to right and the whole expression’s value is that of the last operand. So (a, b, c) evaluates to c, which is 3. (This is the comma operator, not the comma in a variable declaration list.)

8. What does this char-arithmetic program print?

#include <stdio.h>

int main(void) {
    char c = 'A';

    printf("%c\n", c + 1);
    printf("%c\n", 'a' + 1);
    printf("%d\n", 'A' + 1);
    return 0;
}

Output:

B
b
66

Character constants are int values, and arithmetic promotes them, so 'A' + 1 is 66. Printed with %c, 65 + 1 = 66 displays as the character 'B', and 'a' + 1 displays 'b'. Printed with %d, 'A' + 1 shows the numeric value 66.

9. What does this pre/post-increment program print?

#include <stdio.h>

int main(void) {
    int i = 5;

    printf("%d\n", i++);
    printf("%d\n", i);
    printf("%d\n", ++i);
    printf("%d\n", i);
    return 0;
}

Output:

5
6
7
7

i++ yields the old value 5 and then increments i to 6. The next printf shows 6. ++i increments first to 7 and yields the new value 7. The final i is 7. Each printf call is a separate statement, so the evaluation order here is fully well-defined.

10. What does this static-variable program print?

#include <stdio.h>

int next(void) {
    static int count = 0;
    count++;
    return count;
}

int main(void) {
    printf("%d\n", next());
    printf("%d\n", next());
    printf("%d\n", next());
    return 0;
}

Output:

1
2
3

A static local variable is initialized once and retains its value between calls. Each call to next() increments the shared count, producing 1, 2, then 3. (Writing printf("%d %d %d\n", next(), next(), next()) would be a trap: the C standard leaves the order in which function arguments are evaluated unspecified, so that line’s output is not portable.)

11. What does this array/pointer sizeof print?

#include <stdio.h>

int main(void) {
    int arr[10];

    printf("%zu\n", sizeof(arr));
    printf("%zu\n", sizeof(arr[0]));
    return 0;
}

Output:

40
4

Inside the same function, arr is a real array: sizeof(arr) is 10 * sizeof(int) = 40. sizeof(arr[0]) is sizeof(int) = 4. The trap is different when arr is a function parameter — there it decays to a pointer and sizeof returns the pointer size (see question 12).

12. What does this function-parameter sizeof print?

#include <stdio.h>

void print_size(int a[10]) {
    printf("%zu\n", sizeof(a));
    printf("%zu\n", sizeof(a[0]));
}

int main(void) {
    int arr[10];
    print_size(arr);
    return 0;
}

Output:

8
4

In a function parameter, int a[10] is adjusted to int *a — the array decays to a pointer. So inside print_size, sizeof(a) is sizeof(int *) = 8 on a 64-bit platform, not 40. sizeof(a[0]) is still sizeof(int) = 4. This “arrays decay to pointers in parameters” gotcha is the most-asked C sizeof question.

13. What does this pointer arithmetic print?

#include <stdio.h>

int main(void) {
    int arr[] = {10, 20, 30, 40};
    int *p = arr;

    printf("%d\n", *p);
    printf("%d\n", *(p + 2));
    printf("%d\n", *p + 2);
    printf("%ld\n", (long)(p + 3 - arr));
    return 0;
}

Output:

10
30
12
3

Pointer arithmetic scales by the size of the pointed-to type. *(p + 2) skips two ints to element 30. *p + 2 dereferences first (10) then adds 2 — the parentheses matter. p + 3 - arr is the number of elements between them, which is 3, not a byte count.

14. What does this switch program print?

#include <stdio.h>

int main(void) {
    int x = 2;
    switch (x) {
        case 1:
            printf("one\n");
        case 2:
            printf("two\n");
        case 3:
            printf("three\n");
        default:
            printf("default\n");
    }
    return 0;
}

Output:

two
three
default

switch falls through to the next case unless there is a break. x = 2 matches case 2, prints "two", then continues into case 3 and default — printing all three. Forgetting break is the classic C bug; interviewers test whether you remember that fall-through is the default behavior.

15. What does this do-while program print?

#include <stdio.h>

int main(void) {
    int i = 0;
    do {
        printf("%d ", i);
        i++;
    } while (i < 3);
    printf("\n");
    return 0;
}

Output:

0 1 2 

A do-while loop runs its body at least once before checking the condition. Here it prints 0, 1, 2 and stops when i reaches 3. If this were a plain while (i < 3) with the check first, the output would be identical for this input — the difference shows only when the initial condition is already false.

My Private Notes

Notes are auto-saved locally to this device.