Back to Practice C Programming

Preprocessor Directives - Practice MCQs for CCAT

50 Questions Section B: Programming C Programming

Preprocessor Directives Question Bank for C-CAT

Topic-wise Preprocessor Directives MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
Which symbol is used to start a preprocessor directive?
A@
B#
C$
D&
Show Answer & Explanation

Correct Answer: B - #

All preprocessor directives in C start with the # symbol (e.g., #include, #define).

Q2.
What is the purpose of #include directive?
ADefine a constant
BCreate a macro
CInclude header file
DConditional compilation
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.

Q3.
What is the purpose of #define directive?
AInclude files
BDefine macros/constants
CConditional compilation
DUndefine macros
Show Answer & Explanation

Correct Answer: B - Define macros/constants

#define is used to create macros or symbolic constants that are replaced during preprocessing.

Q4.
What does #ifdef check?
AIf file exists
BIf value is true
CIf macro is defined
DIf function exists
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.

Q5.
What is the difference between #include <file.h> and #include "file.h"?
ANo difference
B<> searches system directories first, "" searches current directory first
C"" is for system files
DThey are interchangeable
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.

Q6.
What is the purpose of #ifndef directive?
AIf defined
BIf value is zero
CIf not defined
DInclude file
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.

Q7.
What is a header guard?
APassword protection
BSyntax checker
CSecurity feature
DPrevents multiple inclusion of header
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.

Q8.
What does #undef do?
ADefines a macro
BEnds if block
CIncludes a file
DUndefines a previously defined macro
Show Answer & Explanation

Correct Answer: D - Undefines a previously defined macro

#undef removes a macro definition that was previously created with #define.

Q9.

What is the output?

#define SQUARE(x) x*x
printf("%d", SQUARE(2+3));
A25
BCompilation error
C10
D11
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.

Q10.
What is the purpose of #pragma directive?
ADefine constants
BCreate loops
CInclude files
DGive compiler-specific instructions
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.

Q11.
Which predefined macro gives the current line number?
A__FILE__
B__LINE__
C__DATE__
D__TIME__
Show Answer & Explanation

Correct Answer: B - __LINE__

__LINE__ is a predefined macro that expands to the current line number in the source file.

Q12.
What does #error directive do?
AHandles runtime errors
BCatches exceptions
CLogs errors
DGenerates compilation error with message
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.

Q13.
What is a macro in C?
AA function
BText substitution by preprocessor
CA data type
DA loop
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.

Q14.
Which operator is used for token pasting in macros?
A#
B##
C@
D&&
Show Answer & Explanation

Correct Answer: B - ##

The ## operator concatenates two tokens in macro expansion. Example: #define CONCAT(a,b) a##b

Q15.
What does the # operator do in macros?
AConcatenates tokens
BComments code
CConverts argument to string
DIncludes files
Show Answer & Explanation

Correct Answer: C - Converts argument to string

The # operator (stringizing operator) converts a macro argument to a string literal.

Q16.
What is conditional compilation?
ACompiling conditions
BLoop compilation
CIf-else statements
DIncluding/excluding code based on conditions
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.

Q17.
Which predefined macro gives the compilation date?
A__FILE__
B__DATE__
C__LINE__
D__TIME__
Show Answer & Explanation

Correct Answer: B - __DATE__

__DATE__ expands to a string containing the date of compilation in "Mmm dd yyyy" format.

Q18.
What is the advantage of using macros over functions?
AType safety
BNo function call overhead
CBetter debugging
DSmaller code size
Show Answer & Explanation

Correct Answer: B - No function call overhead

Macros are expanded inline, avoiding function call overhead. However, they lack type safety.

Q19.
What does #elif directive do?
AEnd if block
BDefine macro
CElse if condition
DInclude file
Show Answer & Explanation

Correct Answer: C - Else if condition

#elif combines else and if into one directive for chained conditional compilation.

Q20.
What is __FILE__ macro used for?
AFile size
BFile type
CFile count
DCurrent source file name
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.

Q21.
What is the C preprocessor?
AA compiler optimization step
BA debugger
CA tool that processes source code before compilation
DA linker
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.

Q22.
What does #include <stdio.h> do?
ALinks the stdio library
BCompiles stdio.h separately
CCopies the contents of stdio.h into the source file
DCreates a reference to stdio functions
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.

Q23.
What is the difference between #include <file.h> and #include "file.h"?
ANo difference
BAngle brackets search system directories first; quotes search the current directory first
CQuotes are for C++ only
DAngle brackets are for user files
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.

Q24.

What is the output?

#define SQUARE(x) ((x) * (x))
printf("%d", SQUARE(5));
A10
BCompilation error
C5
D25
Show Answer & Explanation

Correct Answer: D - 25

The macro SQUARE(5) expands to ((5) * (5)) = 25. The parentheses around x prevent operator precedence issues.

Q25.

What is wrong with this macro?

#define SQUARE(x) x * x
printf("%d", SQUARE(3 + 1));
AIt expands to 3 + 1 * 3 + 1 = 7
BNothing wrong, output is 16
CCompilation error
DRuntime error
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.

Q26.
What does #define PI 3.14159 create?
AA constant variable
BA global variable
CA macro that replaces PI with 3.14159 during preprocessing
DA function
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.

Q27.
What is the purpose of #ifdef and #endif?
ALoop control
BError handling
CConditional compilation - code is compiled only if the macro is defined
DFunction declaration
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.

Q28.

What is the output?

#define MAX(a, b) ((a) > (b) ? (a) : (b))
printf("%d", MAX(10, 20));
A20
B10
C30
DCompilation error
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.

Q29.
What does #undef do?
ARemoves a function
BDeletes a variable
CUndefines a previously defined macro
DUndoes the last compilation
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.

Q30.

What is the purpose of include guards?

#ifndef HEADER_H
#define HEADER_H
// declarations
#endif
ATo encrypt header files
BTo speed up compilation
CTo prevent multiple inclusions of the same header file
DTo check for syntax errors
Show 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.

Q31.

What does the stringizing operator # do in macros?

#define STR(x) #x
printf("%s", STR(Hello));
AConcatenates strings
BComments out code
CConverts the macro argument to a string literal
DCreates a character
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".

Q32.

What does the token-pasting operator ## do?

#define CONCAT(a, b) a##b
int xy = 100;
printf("%d", CONCAT(x, y));
AAdds a and b
BCompilation error
CCreates a string
DConcatenates tokens a and b into a single token
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.

Q33.
What does __FILE__ expand to?
AThe current line number
BThe function name
CThe compilation date
DThe name of the current source file as a string
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.

Q34.
What does __LINE__ expand to?
AThe current line number in the source file
BThe total number of lines in the file
CThe number of functions
DThe line count of the header
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.

Q35.

What is the output?

#define PRINT(val) printf("Value: %d\n", val)
PRINT(42);
AValue: val
BCompilation error
C42
DValue: 42
Show Answer & Explanation

Correct Answer: D - Value: 42

The macro PRINT(42) expands to printf("Value: %d\n", 42), which prints "Value: 42".

Q36.

What does #error do?

#ifndef CONFIG_SET
#error "CONFIG_SET must be defined"
#endif
ACauses a compilation error with the specified message
BPrints a warning
CThrows a runtime exception
DLogs an error to a file
Show 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.

Q37.

What is the output?

#define A 10
#define B 20
#define SUM (A + B)
printf("%d", SUM * 2);
A60
B40
C50
DCompilation error
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.

Q38.
What does #pragma once do?
AEnsures the header file is included only once
BRuns the code only once
CDefines a one-time macro
DCreates a singleton
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.

Q39.

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; }
ANo difference
BMacros are expanded by the preprocessor (text substitution); functions are compiled and called at runtime
CFunctions are faster
DMacros support recursion
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.

Q40.

What is the output?

#define MULTI_LINE \
    printf("Line 1\n"); \
    printf("Line 2\n");
MULTI_LINE
ALine 1 and Line 2 on separate lines
BLine 2
CLine 1
DCompilation error
Show 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.

Q41.

What is the output?

#define VERSION 3
#if VERSION >= 3
    printf("New version\n");
#else
    printf("Old version\n");
#endif
AOld version
BNew version
CBoth
DCompilation error
Show 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.

Q42.
What does #ifndef stand for?
AIf not defined
BIf no definition found
CIf not a function
DIf no default
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.

Q43.

What is the output?

#define GREET(name) printf("Hello, " #name "!\n")
GREET(World);
AHello, World!
BHello, name!
CHello, #name!
DCompilation error
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!".

Q44.

What is the purpose of #pragma pack?

#pragma pack(push, 1)
struct Packed { char a; int b; };
#pragma pack(pop)
AControls structure member alignment/padding
BEncrypts the structure
COptimizes for speed
DEnables compression
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.

Q45.
What does the predefined macro __DATE__ expand to?
AThe current runtime date
BThe date when the file was compiled as a string
CThe file modification date
DThe system date
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").

Q46.

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);
A10 5
B5 10
CCompilation error
DUndefined
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.

Q47.

What is a variadic macro?

#define LOG(fmt, ...) printf(fmt, __VA_ARGS__)
LOG("Sum: %d\n", 42);
AA macro with no arguments
BA recursive macro
CA macro that returns multiple values
DA macro that accepts a variable number of arguments
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.

Q48.

What is the result of #if 0?

#if 0
printf("This line");
#endif
AThe code block is excluded from compilation
BPrints 'This line'
CCompilation error
DRuntime error
Show 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.

Q49.

What does the defined operator do in preprocessor directives?

#if defined(DEBUG) && defined(VERBOSE)
    printf("Debug verbose mode\n");
#endif
ADefines new macros
BChecks whether macros are defined, returning 1 or 0
CCreates type definitions
DLinks libraries
Show 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.

Q50.

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));
}
A7
BCompilation error
C34
DUndefined
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.

Showing 1-10 of 50 questions