Practice 20 Preprocessor Directives multiple-choice questions designed for CDAC CCAT exam preparation. Click "Show Answer" to reveal the correct option with detailed explanation.
Show Answer & Explanation
Correct Answer: B — #
All preprocessor directives in C start with the # symbol (e.g., #include, #define).
Show Answer & Explanation
Correct Answer: B — Include header file
The #include directive is used to include the contents of another file (usually header files) in the current source file.
Show Answer & Explanation
Correct Answer: B — Define macros/constants
#define is used to create macros or symbolic constants that are replaced during preprocessing.
Show Answer & Explanation
Correct Answer: B — If macro is defined
#ifdef checks if a macro has been defined. If defined, the code block is included in compilation.
Show Answer & Explanation
Correct Answer: B — <> searches system directories first, "" searches current directory first
Angle brackets <> search system include directories first, while quotes "" search the current directory first, then system directories.
Show Answer & Explanation
Correct Answer: B — If not defined
#ifndef means "if not defined". It checks if a macro is NOT defined and is commonly used for header guards.
Show Answer & Explanation
Correct Answer: B — Prevents multiple inclusion of header
Header guards (#ifndef, #define, #endif pattern) prevent a header file from being included multiple times.
Show Answer & Explanation
Correct Answer: B — Undefines a previously defined macro
#undef removes a macro definition that was previously created with #define.
What is the output?
`#define SQUARE(x) x*x`
`printf("%d", SQUARE(2+3));`Show Answer & Explanation
Correct Answer: B — 11
SQUARE(2+3) expands to 2+3*2+3 = 2+6+3 = 11. Use parentheses: #define SQUARE(x) ((x)*(x)) for correct result.
Show Answer & Explanation
Correct Answer: B — Give compiler-specific instructions
#pragma provides special instructions to the compiler. Common uses include disabling warnings or setting alignment.
Show Answer & Explanation
Correct Answer: B — __LINE__
__LINE__ is a predefined macro that expands to the current line number in the source file.
Show Answer & Explanation
Correct Answer: B — Generates compilation error with message
#error causes the compiler to stop and display the specified error message during preprocessing.
Show Answer & Explanation
Correct Answer: B — Text substitution by preprocessor
A macro is a fragment of code defined with #define that is substituted by the preprocessor before compilation.
Show Answer & Explanation
Correct Answer: B — ##
The ## operator concatenates two tokens in macro expansion. Example: #define CONCAT(a,b) a##b
Show Answer & Explanation
Correct Answer: B — Converts argument to string
The # operator (stringizing operator) converts a macro argument to a string literal.
Show Answer & Explanation
Correct Answer: B — Including/excluding code based on conditions
Conditional compilation uses #if, #ifdef, #ifndef, #else, #elif, #endif to include/exclude code during preprocessing.
Show Answer & Explanation
Correct Answer: C — __DATE__
__DATE__ expands to a string containing the date of compilation in "Mmm dd yyyy" format.
Show Answer & Explanation
Correct Answer: B — No function call overhead
Macros are expanded inline, avoiding function call overhead. However, they lack type safety.
Show Answer & Explanation
Correct Answer: B — Else if condition
#elif combines else and if into one directive for chained conditional compilation.
Show Answer & Explanation
Correct Answer: B — Current source file name
__FILE__ expands to a string containing the name of the current source file being compiled.