Preprocessor & Macros Questions with Answers

22 practice questions on Preprocessor & Macros (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 22 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

#include <stdio.h>

#define ADD(a, b) a + b

int main(void) {
    printf("%d\n", 2 * ADD(3, 4));
    return 0;
}
  1. 24
  2. 10
  3. 11
  4. 14

Answer: B. 10

Explanation

The expansion is 2 * 3 + 4, and * binds tighter, so the result is 6 + 4 = 10, not 2 * 7.

Step-by-step solution

  1. The macro is text: 2 * ADD(3, 4) becomes 2 * 3 + 4.
  2. Binds tighter than +, so 2 * 3 = 6 is computed first.
  3. Then 6 + 4 = 10: option B.

Why the other options are wrong

  • A. 24 does not come from this expansion; the correct value is 10.
  • C. 11 is wrong; 2 * 3 is 6, and 6 + 4 is 10.
  • D. 14 would be 2 * 7, which needs the body wrapped in parentheses; the macro is not.

Tip: Wrap the macro body and each parameter in parentheses so the result groups the way you expect.

Question 2Easy

What is the output?

#include <stdio.h>

#define X 10
#undef X
#define X 20

int main(void) {
    printf("%d\n", X);
    return 0;
}
  1. 20
  2. 30
  3. 10
  4. Compilation error

Answer: A. 20

Explanation

#undef removes the first definition, so X can be redefined; from that point on X expands to 20.

Step-by-step solution

  1. #define X 10 is set first, then #undef X removes it.
  2. #define X 20 defines X again with the new value.
  3. Printf prints X as 20: option A.

Why the other options are wrong

  • B. 30 is not defined anywhere in the code.
  • C. 10 was removed by #undef before the use.
  • D. Redefining a macro after #undef is valid C, so there is no error.

Tip: #undef lets a macro be redefined; the value comes from the most recent definition.

Question 3Easy

What is the output?

#include <stdio.h>

#define STR(x) #x

int main(void) {
    printf("%s\n", STR(hello world));
    return 0;
}
  1. hello world
  2. Compilation error
  3. STR(hello world)
  4. x

Answer: A. hello world

Explanation

The # operator turns the macro argument into a string literal, so STR(hello world) becomes "hello world".

Step-by-step solution

  1. # stringizes the argument.
  2. STR(hello world) becomes the string hello world.

Why the other options are wrong

  • B. Valid macro.
  • C. Not literal.
  • D. Replaces x.

Tip: # = to string.

Question 4Easy

What is the output?

#include <stdio.h>

#define NAME "Ravi"

int main(void) {
    printf("NAME\n");
    printf("%s\n", NAME);
    return 0;
}
  1. Ravi on both lines
  2. Ravi on the first line, NAME on the second
  3. NAME on both lines
  4. NAME on the first line, Ravi on the second

Answer: D. NAME on the first line, Ravi on the second

Explanation

Macros are never replaced inside string literals, so the first printf prints NAME literally; the bare NAME in the second call expands to "Ravi".

Step-by-step solution

  1. Macros are not replaced inside string literals.
  2. The first printf prints NAME.
  3. The second uses NAME as the macro: Ravi.

Why the other options are wrong

  • A. Text in quotes is not replaced.
  • B. The order is reversed.
  • C. The macro is substituted in the second call.

Tip: No substitution in quotes.

Question 5Medium

What is the output?

#include <stdio.h>

#define CAT(a, b) a##b

int main(void) {
    int xy = 7;
    printf("%d\n", CAT(x, y));
    return 0;
}
  1. 0
  2. xy
  3. 7
  4. Compilation error

Answer: C. 7

Explanation

## pastes the two tokens into the single identifier xy, which is the variable holding 7.

Step-by-step solution

  1. The ## operator pastes its two arguments into one token.
  2. CAT(x, y) becomes the single identifier xy.
  3. Xy is a variable holding 7, so the output is 7: option C.

Why the other options are wrong

  • A. Xy is a declared variable with value 7, so it is not 0.
  • B. The program prints the variable's value, not its name.
  • D. The code is valid; ## creates a valid identifier that exists.

Tip: Token pasting with ## builds a new identifier before compilation; check that the result is a name that exists.

Question 6Medium

What is the output?

#include <stdio.h>

#define DEBUG 0

int main(void) {
#ifdef DEBUG
    printf("A");
#endif
#if DEBUG
    printf("B");
#endif
    printf("\n");
    return 0;
}
  1. AB
  2. Nothing is printed
  3. B
  4. A

Answer: D. A

Explanation

#ifdef only checks that DEBUG is defined (it is, as 0), so A prints. #if evaluates its value, 0, so B is skipped.

Step-by-step solution

  1. #ifdef DEBUG checks only whether the name is defined; it is defined (as 0), so the A line is kept.
  2. #if DEBUG evaluates the value 0, which is false, so the B line is removed.
  3. Only A is printed: option D.

Why the other options are wrong

  • A. B is not printed because #if DEBUG evaluates to 0.
  • B. A is printed because DEBUG is defined.
  • C. B is removed because its value is 0.

Tip: #ifdef tests whether a name exists; #if tests its numeric value.

Question 7Medium

What is the output?

#include <stdio.h>

#define TRIPLE(x) (3 * x)

int main(void) {
    int a = 3;
    printf("%d\n", TRIPLE(a + 1));
    return 0;
}
  1. 10
  2. 13
  3. 12
  4. 9

Answer: A. 10

Explanation

The body has outer parentheses but the parameter does not: (3 * a + 1) = 9 + 1 = 10. It should be written (3 * (x)).

Step-by-step solution

  1. The body is (3 * x), but the parameter is not wrapped, so a + 1 is pasted in as text.
  2. The expansion is (3 * a + 1).
  3. With a = 3: 3 * 3 + 1 = 9 + 1 = 10: option A.

Why the other options are wrong

  • B. 13 does not match the expansion; the correct value is 10.
  • C. 12 is 3 * (a + 1) = 3 * 4, which needs the parameter wrapped in parentheses.
  • D. 9 is 3 * a and drops the + 1 that the expansion adds.

Tip: Put parentheses around every macro parameter as well as around the whole body.

Question 8Medium

What does the #error directive do when the preprocessor reaches it?

  1. It disables all warnings in the file
  2. It throws a run-time exception
  3. It makes compilation fail and prints the given message
  4. It prints the message when the program runs

Answer: C. It makes compilation fail and prints the given message

Explanation

#error is a diagnostic directive; reaching it (for example inside an #if checking a required setting) stops translation with the message.

Step-by-step solution

  1. #error is a preprocessor directive.
  2. When the preprocessor reaches it, translation stops and the message is shown.
  3. So compilation fails and prints the given text: option C.

Why the other options are wrong

  • A. #error does not change warnings.
  • B. Nothing runs at program run time; the error happens during compilation.
  • D. The message appears at compile time, not when the program runs.

Tip: Use #error inside #if to check that required settings are present.

Question 9Hard

What is the output?

#include <stdio.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main(void) {
    int i = 5, j = 3;
    int m = MAX(i++, j);
    printf("%d %d\n", i, m);
    return 0;
}
  1. 6 6
  2. 6 5
  3. 7 6
  4. 7 5

Answer: C. 7 6

Explanation

The expansion is ((i++) > (j)? (i++): (j)).

The test uses 5 (i becomes 6), then the chosen branch i++ yields 6 and makes i 7.

A function would evaluate i++ once.

Step-by-step solution

  1. The expansion is ((i++) > (j)? (i++): (j)), so i++ runs in the test.
  2. Test: i is 5, so 5 > 3 is true and i becomes 6.
  3. The true branch runs i++ again: it yields 6 and makes i 7, so m = 6.
  4. Output is 7 6: option C.

Why the other options are wrong

  • A. I is incremented twice, once in the test and once in the branch, so it ends at 7, not 6.
  • B. I ends at 7 because the branch increment also runs; m is 6, not 5.
  • D. M is 6, not 5, because the branch yields i++ before the increment.

Tip: A macro pastes its arguments as text, so side effects like i++ can run more than once, unlike in a function.

Question 10Hard

What is the output?

#include <stdio.h>

#define SQ(x) (x) * (x)

int main(void) {
    printf("%d\n", 100 / SQ(5));
    return 0;
}
  1. 500
  2. 100
  3. 4
  4. 25

Answer: B. 100

Explanation

The expansion is 100 / (5) * (5); / and * group left to right, so it is (100 / 5) * 5 = 100, not 100 / 25 = 4. The whole body also needs outer parentheses.

Step-by-step solution

  1. The expansion is 100 / (5) * (5).
  2. / and * have the same precedence and group left to right.
  3. So it is (100 / 5) * 5 = 20 × 5 = 100: option B.

Why the other options are wrong

  • A. 500 is not produced; the value is 100 divided by 5 and then multiplied by 5.
  • C. 4 comes from 100 / (5 × 5), which the macro does not produce.
  • D. 25 is SQ(5) itself; the expression divides 100 by 5 first, not by 25.

Tip: Wrap the whole macro body in outer parentheses, and remember / and * group left to right.

Practise all 22 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics