ACompressed recap
🧾
The preprocessor rewrites the source before compilation. #define replaces text: expand it completely, then evaluate with normal precedence. Bracket every parameter and the whole body, and never pass side effects. A function pointer int (*f)(int, int) stores a function’s address; call it as f(…) or (*f)(…); arrays of them and callbacks follow.
BMerged rule card
1.8 Preprocessor macros and their expansion traps; function pointers
- Preprocessor runs before compiling: #include, #define, #ifdef.
- #define is text substitution: expand fully, then apply precedence.
- Bracket parameters and the whole body: #define SQ(x) ((x) * (x)).
- Side-effect arguments may be evaluated several times (undefined for ++).
- int (*f)(int, int) = add; f(2, 3) or (*f)(2, 3).
- Function pointer arrays and callbacks; int *f(int) is different.
CInterleaved GATE-style questions
Q1NAT1 markMacro
With #define DOUBLE(x) x + x, what is 3 * DOUBLE(2)?
Numerical answer: ________
Q2NAT2 marksMacro
With #define N 3 and #define F N + 1, what is F * F?
Numerical answer: ________
Q3MCQ2 marksOutput
What does this print?
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define N 4
#define AREA N * N + 1
int main(void)
{
int x = 3, y = 7;
printf("%d %d %d\n", MAX(x, y), 2 * AREA, MAX(x + 1, y - 4));
return 0;
}
- (A)
7 33 3 - (B)
7 33 4 - (C)
7 34 4 - (D)
3 33 4
Q4MCQ1 markFunction pointer call
If int (*f)(int) = g;, which calls g with 5?
- (A)
*f(5) - (B)
f(5) - (C)
f[5] - (D)
&f(5)
Q5MCQ1 markBrackets
Which definition makes 1000 / SQ(10) equal to 10?
- (A)
#define SQ(x) (x) * (x) - (B)
#define SQ(x) (x * x)and also(x)unused - (C)
#define SQ(x) x * x - (D)
#define SQ(x) ((x) * (x))
Q6MSQ2 marksFacts
Which are true?
- (A) A function-like macro is type-checked like a function.
- (B) Function pointers can be stored in arrays.
- (C) #define creates no variable in memory.
- (D) A macro can be redefined after #undef.
Q7MCQ2 marksOutput
What does this print?
#define PROD(a, b) a * b
#define DOUBLE(x) x + x
int main(void)
{
int k = 2;
printf("%d %d\n", PROD(k + 1, k + 2), 3 * DOUBLE(k));
return 0;
}
- (A)
6 12 - (B)
12 12 - (C)
6 8 - (D)
7 8
Q8NAT1 markCallback
With apply(mul, 6, 7) from Lesson 1.8, what is returned?
Numerical answer: ________
DAnswer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: 81 mark
3 * 2 + 2 = 8.
Re-read: Lesson 1.8 — Preprocessor macros and their expansion traps; function pointers
Q2 Answer: 72 marks
N + 1 * N + 1 = 3 + 3 + 1 = 7.
Re-read: Lesson 1.8 — Preprocessor macros and their expansion traps; function pointers
Q3 Answer: (B)2 marks
Expansions in Lesson 1.8.
Output
7 33 4
- (A) is wrong: x + 1 = 4 is larger than y − 4 = 3.
- (C) is wrong: 2 * 4 * 4 + 1 = 33.
- (D) is wrong: MAX(3, 7) = 7.
Re-read: Lesson 1.8 — Preprocessor macros and their expansion traps; function pointers
Q4 Answer: (B)1 mark
A function pointer is called like the function; (*f)(5) also works.
- (A) is wrong: Parses as *(f(5)).
- (C) is wrong: f is not an array.
- (D) is wrong: Not a call of f.
Re-read: Lesson 1.8 — Preprocessor macros and their expansion traps; function pointers
Q5 Answer: (D)1 mark
Only the fully bracketed body divides by 100.
- (A) is wrong: 1000 / (10) * (10) = 1000.
- (B) is wrong: (x * x) works here but breaks for x = a + b; the fully bracketed form is the safe one.
- (C) is wrong: 1000 / 10 * 10 = 1000.
Re-read: Lesson 1.8 — Preprocessor macros and their expansion traps; function pointers
Q6 Answer: (B) (C) (D)2 marks
Macros are not type-checked.
- (A) is wrong: No type checking for macros.
Re-read: Lesson 1.8 — Preprocessor macros and their expansion traps; function pointers
Q7 Answer: (C)2 marks
PROD(k + 1, k + 2) → k + 1 * k + 2 = 2 + 2 + 2 = 6; 3 * DOUBLE(k) → 3 * k + k = 8.
Output
6 8
- (A) is wrong: 3 * k + k is 8, not 3 × 4.
- (B) is wrong: The macros have no brackets, so the products are not (k+1)(k+2) or 3(k+k).
- (D) is wrong: 1 * k is 2, so the first value is 6.
Re-read: Lesson 1.8 — Preprocessor macros and their expansion traps; function pointers
Q8 Answer: 421 mark
mul(6, 7) = 42.
Re-read: Lesson 1.8 — Preprocessor macros and their expansion traps; function pointers