Menu
CoddyTech

C Programming Output Questions

Predict-the-output questions from written tests; each one has a defined result on this build.

13 questions
  1. What does printf return when you nest it inside another printf?

    C
    #include <stdio.h>
    
    int main(void) {
        printf("%d\n", printf("Hello"));
        return 0;
    }

    It prints Hello5. The inner printf prints Hello and returns the number of characters printed, 5. scanf likewise returns how many items it read: check scanf("%d", &n) != 1.

  2. Does sizeof(i++) increment i?

    C
    #include <stdio.h>
    
    int main(void) {
        int i = 1;
        size_t n = sizeof(i++);
        printf("%zu %d\n", n, i);
        return 0;
    }

    It prints 4 1. sizeof needs only the operand's type, so i++ is never evaluated. The exception is a variable length array, whose sizeof is computed at runtime and does evaluate its operand.

  3. Is -1 < 1u true in C?

    C
    #include <stdio.h>
    
    int main(void) {
        int a = -1;
        unsigned int b = 1;
        if (a < b)
            printf("less\n");
        else
            printf("greater\n");
        return 0;
    }

    It prints greater. The int is converted to unsigned, and -1 becomes UINT_MAX. The same bug hits i < strlen(s) - 1 on an empty string; -Wsign-compare warns.

  4. What runs when a switch case has no break?

    C
    #include <stdio.h>
    
    int main(void) {
        int n = 2;
        switch (n) {
        case 1: printf("one ");
        case 2: printf("two ");
        case 3: printf("three "); break;
        default: printf("other ");
        }
        printf("\n");
        return 0;
    }

    It prints two three. Execution jumps to case 2 and falls through into case 3 until its break. Mark deliberate fallthrough with a comment or C23's [[fallthrough]]; so readers and -Wimplicit-fallthrough know.

  5. What happens to a static local variable between function calls?

    C
    #include <stdio.h>
    
    int counter(void) {
        static int count = 0;
        count++;
        return count;
    }
    
    int main(void) {
        printf("%d ", counter());
        printf("%d ", counter());
        printf("%d\n", counter());
        return 0;
    }

    It prints 1 2 3. A static local is initialized once, before the program starts, and keeps its value between calls; a plain local would give 1 1 1. Its initializer must be a constant expression.

  6. Is a float set to 0.1 equal to the literal 0.1?

    C
    #include <stdio.h>
    
    int main(void) {
        float f = 0.1;
        if (f == 0.1)
            printf("equal\n");
        else
            printf("not equal\n");
        return 0;
    }

    It prints not equal. 0.1 has no exact binary form: f holds the nearest float, the literal is the nearest double, and they differ. Compare with a tolerance, such as fabs(a - b) < 1e-6.

  7. How many times does i++ && i++ increment i when i starts at 0?

    C
    #include <stdio.h>
    
    int main(void) {
        int i = 0;
        if (i++ && i++) {
            printf("inside\n");
        }
        printf("%d\n", i);
        return 0;
    }

    It prints 1. The left i++ yields 0 (false), so && short-circuits and the right side never runs. It is defined because && and || have a sequence point after the left operand, which is also what makes p != NULL && p->value > 0 safe.

  8. What happens with a semicolon right after a for loop's parentheses?

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

    It prints i = 3 once. The ; is the loop's empty body, so the loop counts i to 3 and the braces run once afterwards. if (x > 0); has the same bug.

  9. What happens when an unsigned char goes past 255 or below 0?

    C
    #include <stdio.h>
    
    int main(void) {
        unsigned char c = 255;
        c++;
        unsigned char d = 0;
        d--;
        printf("%d %d\n", c, d);
        return 0;
    }

    It prints 0 255. Unsigned arithmetic wraps modulo 2 to the power of the bit width. Signed overflow such as INT_MAX + 1 is undefined behavior, not a guaranteed wrap, and optimizers rely on that.

  10. Is sizeof('a') the same as sizeof(char) in C?

    C
    #include <stdio.h>
    
    int main(void) {
        printf("%zu %zu\n", sizeof(char), sizeof('a'));
        return 0;
    }

    It prints 1 4. In C a character constant like 'a' has type int, so its size is sizeof(int). In C++ it is a char and the size is 1.

  11. How does C parse 1 << 3 + 1 and 6 & 2 == 2?

    C
    #include <stdio.h>
    
    int main(void) {
        int a = 1 << 3 + 1;
        int b = (6 & 2 == 2) + 6;
        printf("%d %d\n", a, b);
        return 0;
    }

    It prints 16 6. + binds tighter than <<, so 1 << 3 + 1 is 1 << 4. == binds tighter than &, so 6 & 2 == 2 is 6 & 1, which is 0. Parenthesize bitwise expressions; -Wall warns about both.

  12. What do 'A' + 2 and '3' - '0' evaluate to?

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

    It prints C 67 3. A char is a small integer, so 'A' + 2 is 67, shown as C by %c. '3' - '0' is 3 because the standard requires the digits to be consecutive; letters are consecutive only in ASCII-based encodings.

    Practice it: To Lower Case

  13. What is the output of i = i++ + ++i;?

    There is none: modifying i twice between sequence points (C11: unsequenced side effects on the same object) is undefined behavior, so any output is allowed.

    TermMeaningExample
    Undefinedno requirements at alli = i++ + 1, signed overflow
    Unspecifiedone of several results, not documentedorder of f() + g()
    Implementation-defineddocumented by the compilersizeof(int), signedness of char

    See undefined behavior.

Coddy programming languages illustration

Learn to code with Coddy

Get started