All posts

C Quiz: 10 Questions, Answers and Code Explanations

Ten C practice questions with explanations, complete examples, and clear answers to common traps involving arrays, operators and undefined behavior.

Practise C with 10 questions on arrays, operators, scope, macros and conversions. Each answer explains the rule behind the result. One question deliberately has undefined behavior: a compiler's printed output is not a reliable answer for that program.

Use C17 for the examples below. Try each question before reading its answer. If you want more exercises, start from the programming practice hub.

1. Which function changes 1.98 to 1.0?

Options: ceil(), floor(), abs(), or fabs(). Answer: `floor()`. It returns the greatest integer value not greater than its input, represented as a floating-point value. Include <math.h>.

#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.0f\n", floor(1.98));
    return 0;
}

For a negative input, floor(-1.98) is -2.0; it does not simply remove the fraction. Some Unix toolchains require -lm when linking math functions.

2. Is 1[arr] valid C?

#include <stdio.h>

int main(void) {
    int arr[2] = {1, 2};
    printf("%d\n", 1[arr]);
    return 0;
}

Answer: `2`. Array subscripting is defined through pointer addition: arr[1] means *(arr + 1), while 1[arr] means *(1 + arr). Both access the second element here. Prefer arr[1] in everyday code because it is clearer. This does not mean that an array and a pointer are the same type. See our array tutorial and indexing examples.

3. What does -1[arr] mean?

#include <stdio.h>

int main(void) {
    int arr[2] = {1, 2};
    printf("%d\n", -1[arr]);
    return 0;
}

Answer: `-2`. Subscripting binds more tightly than unary minus, so this is -(1[arr]), not (-1)[arr]. The expression reads arr[1] and negates its value. Adding parentheses changes the question; a negative index before this array would be out of bounds.

4. Does b = b++ have a predictable result in C?

#include <stdio.h>

int main(void) {
    int a = 0, b = 0;
    a++;
    b = b++;  /* Undefined behavior in C: do not use this. */
    printf("%d %d\n", a, b);
    return 0;
}

Answer: the program has undefined behavior. The assignment and the post-increment modify b without the sequencing C requires. There is no portable output such as 1 0 or 1 1. Use b++; when the intention is to increment it. This corrects the fixed-output answer in the original quiz. The GCC sequence-point warning documentation discusses this class of mistake.

5. What does m += ++s print?

#include <stdio.h>

int main(void) {
    int m = 5, s = 2;
    m += ++s;
    printf("%d %d\n", m, s);
    return 0;
}

Answer: `8 3`. Prefix increment changes s to 3 and supplies that value to the addition. Adding 3 to m makes it 8. Here the two variables are distinct, unlike the unsafe expression in question 4.

6. Which names should you use for your own variables?

Consider the original options: abc, @a123, __a, and _. Use `abc` as the clear, portable choice. @a123 is not a standard C identifier. __a is reserved for the implementation because it starts with two underscores. A lone underscore is lexically an identifier, but names beginning with an underscore are reserved at file scope. Avoid leading underscores in your own names; answer_count communicates more than _ anyway.

7. Does an inner variable change the outer one?

#include <stdio.h>

int main(void) {
    int m = 5;
    {
        int m = 10;
        (void)m;
    }
    printf("%d\n", m);
    return 0;
}

Answer: `5`. The inner declaration introduces a different object with block scope. It hides the outer m inside that block; it does not assign 10 to the outer variable.

8. Do macros guarantee smaller or faster code?

Answer: no. A function-like macro substitutes tokens before compilation. It may duplicate code and can evaluate an argument more than once. A normal function gives type checking, and a compiler may inline a suitable function. The generated code and workload determine performance, not the word "macro".

#include <stdio.h>

static inline int square(int value) {
    return value * value;
}

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

This replaces the original claim that macros necessarily reduce code size. Read the GCC macro pitfalls guide before using macros with expressions that have side effects.

9. Why does an integer counter with i += 0.5 get stuck?

#include <stdio.h>

int main(void) {
    int i;
    for (i = 0; i < 5; i += 0.5) {
        printf("1");
    }
    return 0;
}

Answer: the loop does not advance. On each iteration, i + 0.5 is converted back to int for the assignment. Starting at zero, the fractional part is discarded and i stays zero. The condition remains true. Do not run this example without a way to stop it. Use an integer step, or use a separate integer counter to represent half steps.

10. Is C always faster than Python?

Answer: no language comparison guarantees a faster result for every program. A straightforward loop compiled to native C commonly avoids the per-operation overhead of an equivalent CPython loop. But algorithms, compiler options, input sizes and libraries matter. Python code can also call compiled numerical libraries. Benchmark the actual task with equivalent algorithms and check the results for correctness; do not treat this as a universal speed rule.

How to compile and check the examples

Save a complete example as quiz.c, then compile with warnings enabled:

cc -std=c17 -Wall -Wextra -Wpedantic quiz.c -lm -o quiz
./quiz

Run the defined examples only. Question 4 demonstrates undefined behavior and question 9 demonstrates a non-terminating loop. A successful compilation alone does not make either program safe to run.

Continue practising

Try the C++ practice questions for a comparison of strings, conversions and value parameters. For more C questions, see the C interview questions and explanations and the data structures tutorials.

Reference: C11 committee draft N1570, especially clauses 6.3.1.4, 6.4.2.1, 6.5, 6.5.2.1 and 7.1.3. The examples use rules that also apply to C17.