C Preprocessor Directives - Tricky MCQ

Previous C Preprocessor Directives Next

Tricky Questions on Preprocessor Directives

Basic Level (15 Questions)

1

When does preprocessor execute in compilation process?

Correct Answer: D) All of the above

Preprocessor runs before actual compilation. It performs text substitution, file inclusion, conditional compilation. Output is expanded source code for compiler. Can be invoked separately (gcc -E). Has no knowledge of C syntax.

2

What is the difference between #include "file" and #include <file>?

Correct Answer: D) All of the above

Quotes search current directory then system include paths. Angle brackets search system include paths only. Convention: quotes for project headers, brackets for standard/library headers. Exact search paths compiler-dependent.

3

What is macro vs function advantage?

Correct Answer: D) All of the above

Macros are text substitution - no runtime overhead. Generic - work with any type. Can use compile-time constants. Disadvantages: no type checking, multiple evaluation of arguments, harder to debug, code bloat.

4

What is the problem with #define SQUARE(x) x*x?

Correct Answer: D) All of the above

Macro arguments should be fully parenthesized. SQUARE(2+3) expands to 2+3*2+3 = 2+6+3 = 11 due to precedence. Correct: ((x)*(x)). Also need parentheses around whole macro if used in expression.

5

What is token pasting operator (##)?

Correct Answer: D) All of the above

Token pasting (##) concatenates tokens before evaluation. Useful for creating function/variable names programmatically. Example: #define DECLARE(type, name) type var_##name. Must result in valid token.

6

What is stringizing operator (#)?

Correct Answer: D) All of the above

Stringizing (#) converts macro argument to string literal. Example: #define STR(x) #x makes STR(hello) become "hello". Preserves whitespace and quotes. Useful for assertions, debugging, generating strings from code.

7

What is #pragma directive?

Correct Answer: D) All of the above

#pragma provides compiler-specific directives. Common pragmas: once (header guards), pack (structure alignment), warning (disable/enable). Behavior varies by compiler. Standardized pragmas in C99: STDC FP_CONTRACT, CX_LIMITED_RANGE.

8

What is #error directive?

Correct Answer: D) All of the above

#error stops compilation with custom message. Used to enforce constraints: required macros, platform checks, deprecated features. Similar to static_assert but at preprocessing stage. #warning gives warning without stopping.

9

What is #undef directive?

Correct Answer: D) All of the above

#undef removes macro definition. Needed before redefining macro (avoiding warning). Useful for macros local to header file. After #undef, #ifdef MACRO is false. Doesn't affect identical macro defined elsewhere.

10

What is #line directive?

Correct Answer: D) All of the above

#line changes line number (and optionally filename) for error reporting. Syntax: #line number "filename". Used by tools generating C code to map errors to original source. __LINE__ and __FILE__ reflect #line values.

11

What is defined() operator in #if?

Correct Answer: D) All of the above

defined(MACRO) returns 1 if MACRO defined, 0 otherwise. Used in #if expressions. Allows complex conditions: #if defined(A) && !defined(B). More flexible than #ifdef/#ifndef which only test single macro.

12

What is #if vs #ifdef difference?

Correct Answer: D) All of the above

#if evaluates constant integer expression. #ifdef tests if macro defined (regardless of value). #if defined(MACRO) identical to #ifdef MACRO. #if more flexible for complex conditions, version checks.

13

What is __VA_ARGS__ in variadic macros?

Correct Answer: D) All of the above

__VA_ARGS__ expands to variable arguments in macro. Syntax: #define LOG(fmt, ...) printf(fmt, __VA_ARGS__). C99 feature. Can be empty if no arguments passed (requires ##__VA_ARGS__ extension for GCC).

14

What are predefined macros like __LINE__, __FILE__?

Correct Answer: D) All of the above

Standard predefined macros: __LINE__ (current line), __FILE__ (filename), __DATE__ (compilation date), __TIME__ (compilation time). Compiler-specific: __STDC__, __cplusplus, __GNUC__, _WIN32. Useful for diagnostics, version checks.

15

What is header guard pattern?

Correct Answer: D) All of the above

Header guards prevent duplicate definitions when header included multiple times. Traditional: #ifndef UNIQUE_NAME / #define UNIQUE_NAME / #endif. #pragma once is compiler extension (faster, less error-prone). Unique name typically filename_UPPERCASE_H.

Hard Level (15 Questions)

16

`#define SQR(x) x*x` then `SQR(1+2)` expands to:

Correct Answer: B) 1+2*1+2 = 5

No parentheses: 1+2*1+2 due to macro text replacement.

17

`#define STR(x) #x` then `STR(hi)` produces:

Correct Answer: A) "hi"

# operator stringifies argument to "hi".

18

`#if 1/0` in `#if` expression:

Correct Answer: A) Error in #if (undefined eval)

#if evaluates constant expressions; division by zero is invalid in #if.

19

`__FILE__` and `__LINE__`:

Correct Answer: C) Macros expanding to constants

They are macros expanding to filename string and line number int constant.

20

`#include <stdio.h>` vs `#include "stdio.h"`:

Correct Answer: A) Angle: implementation search; quotes: often local first

Standard specifies different search order for quoted vs angle includes.

21

`#ifdef X` when X defined as 0:

Correct Answer: B) False — defined means macro exists

ifdef tests existence; macro defined to 0 is still defined.

22

`#define f() 42` vs `#define f 42`:

Correct Answer: D) Both B and C

Function-like macro f() needs (); object-like f replaces token f.

23

`##` token pasting `a##b` with `a` token and `b` token:

Correct Answer: A) Creates ab if valid single token

## concatenates tokens into one preprocessing token if valid.

24

`#pragma once` is:

Correct Answer: A) Non-standard but widely supported include guard

#pragma once is common extension; not required by ISO C.

25

Macro recursion `#define A A` expansion:

Correct Answer: A) Infinite rescan stopped — no further replace of A

If macro name appears in its own replacement, it is not rescanned for further replacement.

26

`__COUNTER__` (GCC/Clang):

Correct Answer: A) Increments each use at preprocess time

__COUNTER__ is extension assigning successive integers per use.

27

`#undef X` then `#ifdef X`:

Correct Answer: A) False

#undef removes macro; ifdef fails afterward.

28

`#line` directive:

Correct Answer: A) Sets logical line and optional filename for diagnostics

#line changes __LINE__ and __FILE__ reporting for following lines.

29

Stringify `STR(\n)` gives:

Correct Answer: A) "\n" two-char string backslash-n

Argument is stringified before escape processing becomes "\n" literal in source.

30

`#if defined(X) + defined(Y)` when both defined:

Correct Answer: A) 2

defined returns 0 or 1; sum is 2 when both exist.

Previous C Preprocessor Directives Next