C Programming

Preprocessor Directives — Practice MCQs for CCAT

20 Questions Section B: Programming C Programming

Practice 20 Preprocessor Directives multiple-choice questions designed for CDAC CCAT exam preparation. Click "Show Answer" to reveal the correct option with detailed explanation.

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
BInclude header file
CCreate a macro
DConditional compilation
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.

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 macro is defined
CIf value is true
DIf function exists
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.

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 not defined
CIf value is zero
DInclude file
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.

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

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

Correct Answer: B — 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
B11
C10
DCompilation error
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.

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

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
BGenerates compilation error with message
CLogs errors
DCatches exceptions
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.

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
BConverts argument to string
CComments code
DIncludes files
Show Answer & Explanation

Correct Answer: B — Converts argument to string

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

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

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

Correct Answer: C — __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
BElse if condition
CDefine macro
DInclude file
Show Answer & Explanation

Correct Answer: B — Else if condition

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

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