Preprocessor Directives Question Bank for C-CAT
Topic-wise Preprocessor Directives MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: B - #
All preprocessor directives in C start with the # symbol (e.g., #include, #define).
Show Answer & Explanation
Correct Answer: C - 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: C - 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: C - 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: D - 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: D - 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: D - 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: D - 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: D - 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: C - Converts argument to string
The # operator (stringizing operator) converts a macro argument to a string literal.
Show Answer & Explanation
Correct Answer: D - 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: B - __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: C - Else if condition
#elif combines else and if into one directive for chained conditional compilation.
Show Answer & Explanation
Correct Answer: D - Current source file name
__FILE__ expands to a string containing the name of the current source file being compiled.
Show Answer & Explanation
Correct Answer: C - A tool that processes source code before compilation
The C preprocessor is a text processing tool that runs before the compiler. It handles directives like #include, #define, and #ifdef to transform the source code.
Show Answer & Explanation
Correct Answer: C - Copies the contents of stdio.h into the source file
#include literally copies the contents of the specified header file into the current source file before compilation. Angle brackets search system include directories.
Show Answer & Explanation
Correct Answer: B - Angle brackets search system directories first; quotes search the current directory first
#include <file.h> searches system/standard include directories. #include "file.h" searches the current directory first, then falls back to system directories.
What is the output?
#define SQUARE(x) ((x) * (x))
printf("%d", SQUARE(5));Show Answer & Explanation
Correct Answer: D - 25
The macro SQUARE(5) expands to ((5) * (5)) = 25. The parentheses around x prevent operator precedence issues.
What is wrong with this macro?
#define SQUARE(x) x * x
printf("%d", SQUARE(3 + 1));Show Answer & Explanation
Correct Answer: A - It expands to 3 + 1 * 3 + 1 = 7
Without parentheses, SQUARE(3+1) expands to 3 + 1 * 3 + 1. Due to precedence, 1*3 is evaluated first, giving 3+3+1 = 7, not the expected 16.
Show Answer & Explanation
Correct Answer: C - A macro that replaces PI with 3.14159 during preprocessing
#define creates a macro. The preprocessor replaces every occurrence of PI with 3.14159 as text substitution before compilation.
Show Answer & Explanation
Correct Answer: C - Conditional compilation - code is compiled only if the macro is defined
#ifdef checks if a macro is defined. If true, the code between #ifdef and #endif is included in compilation; otherwise it is excluded.
What is the output?
#define MAX(a, b) ((a) > (b) ? (a) : (b))
printf("%d", MAX(10, 20));Show Answer & Explanation
Correct Answer: A - 20
MAX(10, 20) expands to ((10) > (20) ? (10) : (20)). Since 10 is not greater than 20, the result is 20.
Show Answer & Explanation
Correct Answer: C - Undefines a previously defined macro
#undef removes a previously defined macro. After #undef MACRO_NAME, the identifier is no longer recognized as a macro.
What is the purpose of include guards?
#ifndef HEADER_H
#define HEADER_H
// declarations
#endifShow Answer & Explanation
Correct Answer: C - To prevent multiple inclusions of the same header file
Include guards prevent a header file from being included more than once in a compilation unit, which would cause duplicate definition errors.
What does the stringizing operator # do in macros?
#define STR(x) #x
printf("%s", STR(Hello));Show Answer & Explanation
Correct Answer: C - Converts the macro argument to a string literal
The # operator converts a macro parameter into a string literal. STR(Hello) expands to "Hello".
What does the token-pasting operator ## do?
#define CONCAT(a, b) a##b
int xy = 100;
printf("%d", CONCAT(x, y));Show Answer & Explanation
Correct Answer: D - Concatenates tokens a and b into a single token
The ## operator concatenates two tokens into one. CONCAT(x, y) becomes xy, which refers to the variable xy with value 100.
Show Answer & Explanation
Correct Answer: D - The name of the current source file as a string
__FILE__ is a predefined macro that expands to the name of the current source file as a string literal. It is useful for debugging and error messages.
Show Answer & Explanation
Correct Answer: A - The current line number in the source file
__LINE__ is a predefined macro that expands to the current line number in the source file as an integer constant.
What is the output?
#define PRINT(val) printf("Value: %d\n", val)
PRINT(42);Show Answer & Explanation
Correct Answer: D - Value: 42
The macro PRINT(42) expands to printf("Value: %d\n", 42), which prints "Value: 42".
What does #error do?
#ifndef CONFIG_SET
#error "CONFIG_SET must be defined"
#endifShow Answer & Explanation
Correct Answer: A - Causes a compilation error with the specified message
#error forces the compiler to emit an error with the given message and stop compilation. It is used to enforce compile-time requirements.
What is the output?
#define A 10
#define B 20
#define SUM (A + B)
printf("%d", SUM * 2);Show Answer & Explanation
Correct Answer: A - 60
SUM expands to (A + B) which expands to (10 + 20). So SUM * 2 becomes (10 + 20) * 2 = 30 * 2 = 60. The parentheses in the macro definition ensure correct evaluation.
Show Answer & Explanation
Correct Answer: A - Ensures the header file is included only once
#pragma once is a non-standard but widely supported directive that prevents a header file from being included more than once. It serves the same purpose as include guards.
What is the difference between a macro and a function?
#define ADD(a,b) ((a)+(b))
int add(int a, int b) { return a+b; }Show Answer & Explanation
Correct Answer: B - Macros are expanded by the preprocessor (text substitution); functions are compiled and called at runtime
Macros are replaced by the preprocessor as text substitution before compilation, with no type checking. Functions are compiled separately and involve a call overhead but provide type safety.
What is the output?
#define MULTI_LINE \
printf("Line 1\n"); \
printf("Line 2\n");
MULTI_LINEShow Answer & Explanation
Correct Answer: A - Line 1 and Line 2 on separate lines
The backslash (\) at the end of a line continues the macro definition to the next line. The macro expands to both printf statements, printing both lines.
What is the output?
#define VERSION 3
#if VERSION >= 3
printf("New version\n");
#else
printf("Old version\n");
#endifShow Answer & Explanation
Correct Answer: B - New version
#if evaluates the constant expression at preprocessing time. Since VERSION (3) >= 3 is true, the code printf("New version\n") is included in compilation.
Show Answer & Explanation
Correct Answer: A - If not defined
#ifndef stands for 'if not defined'. It checks whether a macro has NOT been defined. It is the opposite of #ifdef.
What is the output?
#define GREET(name) printf("Hello, " #name "!\n")
GREET(World);Show Answer & Explanation
Correct Answer: A - Hello, World!
The # operator stringizes the argument. #name becomes "World". Adjacent string literals are concatenated by the compiler, giving "Hello, World!".
What is the purpose of #pragma pack?
#pragma pack(push, 1)
struct Packed { char a; int b; };
#pragma pack(pop)Show Answer & Explanation
Correct Answer: A - Controls structure member alignment/padding
#pragma pack controls the alignment of structure members. pack(1) removes all padding, making the structure use minimum memory at the potential cost of performance.
Show Answer & Explanation
Correct Answer: B - The date when the file was compiled as a string
__DATE__ expands to a string literal representing the date of compilation in the format "Mmm dd yyyy" (e.g., "May 31 2026").
What is the output?
#define SWAP(a, b, type) { type t=a; a=b; b=t; }
int x=5, y=10;
SWAP(x, y, int);
printf("%d %d", x, y);Show Answer & Explanation
Correct Answer: A - 10 5
The SWAP macro expands to a block that swaps the values using a temporary variable of the specified type. After expansion, x becomes 10 and y becomes 5.
What is a variadic macro?
#define LOG(fmt, ...) printf(fmt, __VA_ARGS__)
LOG("Sum: %d\n", 42);Show Answer & Explanation
Correct Answer: D - A macro that accepts a variable number of arguments
A variadic macro uses ... to accept a variable number of arguments. __VA_ARGS__ is replaced by the extra arguments passed to the macro.
What is the result of #if 0?
#if 0
printf("This line");
#endifShow Answer & Explanation
Correct Answer: A - The code block is excluded from compilation
#if 0 evaluates to false, so the enclosed code is excluded from compilation. This is commonly used as a way to comment out blocks of code.
What does the defined operator do in preprocessor directives?
#if defined(DEBUG) && defined(VERBOSE)
printf("Debug verbose mode\n");
#endifShow Answer & Explanation
Correct Answer: B - Checks whether macros are defined, returning 1 or 0
The defined operator returns 1 if the specified macro is defined, 0 otherwise. It can be combined with logical operators in #if directives for complex conditional compilation.
What is the output?
#define MAKE_FUNC(name) int name(int a, int b) { return a + b; }
MAKE_FUNC(add)
int main() {
printf("%d", add(3, 4));
}Show Answer & Explanation
Correct Answer: A - 7
The MAKE_FUNC macro generates a complete function definition. MAKE_FUNC(add) expands to a function named add that returns the sum of two integers. add(3, 4) returns 7.