Which directive is used to conditionally compile code based on a logical condition?
A#ifdef
B#if
C#define
D#pragma
Correct Answer:
B. #if
EXPLANATION
#if evaluates constant expressions for conditional compilation. #ifdef checks if a macro is defined, #define creates macros, and #pragma provides compiler-specific directives.
What is the primary purpose of the C preprocessor?
ATo process source code before compilation and perform text substitutions
BTo execute code at runtime
CTo allocate memory dynamically
DTo handle exception management
Correct Answer:
A. To process source code before compilation and perform text substitutions
EXPLANATION
The preprocessor is a text processing tool that processes source code before actual compilation, handling directives like #define, #include, and macros.
What is the purpose of defined() operator in preprocessor conditionals?
ATo check if a macro is defined
BTo define a new macro conditionally
CTo undefine a macro
DTo check if a variable is initialized
Correct Answer:
A. To check if a macro is defined
EXPLANATION
defined(MACRO) is used in #if and #elif directives to test if a macro is defined. Example: #if defined(DEBUG) ... #endif. It's equivalent to #ifdef but can be used in expressions with logical operators like && and ||.
What happens when #if 0...#endif wraps a block of code?
AThe code is compiled but not executed
BThe code is completely ignored by the preprocessor and not compiled
CIt causes a compilation error
DThe code is commented out
Correct Answer:
B. The code is completely ignored by the preprocessor and not compiled
EXPLANATION
#if 0...#endif is used to disable code during preprocessing. The enclosed code is skipped entirely and doesn't appear in the compiled output. This is more efficient than C-style comments for large blocks and maintains nested comment compatibility.
What is the difference between #include and #include "file.h"?
ABoth search in the same directories
B searches system paths first; "" searches current directory first
C"" is only for local files, for system files
DThere is no functional difference; both are equivalent
Correct Answer:
B. searches system paths first; "" searches current directory first
EXPLANATION
#include <file.h> searches in standard system include directories. #include "file.h" searches in the current working directory first, then system directories. This allows you to override system headers with local versions.
Which header file is required to use assert() macro in C?
Aassert.h
Bstdlib.h
Cstdio.h
Derror.h
Correct Answer:
A. assert.h
EXPLANATION
assert.h contains the assert() macro which is useful for debugging. It tests a condition and aborts the program if the condition is false. This macro is only active when NDEBUG is not defined.
In C99, which of the following is a valid use of token pasting operator (##)?
A#define CONCAT(a,b) a##b
B#define CONCAT(a,b) a ## b
C#define CONCAT(a,b) a## ##b
DBoth A and B
Correct Answer:
D. Both A and B
EXPLANATION
Both options A and B are valid. The ## operator pastes two tokens together. Whitespace around ## doesn't matter. #define CONCAT(a,b) a##b could be used as CONCAT(var, 1) to create var1.