How does the #ifdef
directive work in C?
a) It conditionally includes code based on whether a macro is defined
b) It includes a header file
c) It allocates memory
d) It declares a variable
Answer:
a) It conditionally includes code based on whether a macro is defined
Explanation:
The #ifdef
(if defined) directive in C is used to conditionally include or exclude parts of the code based on whether a specific macro is defined. If the macro is defined, the code block following the #ifdef
directive is included in the compilation; otherwise, it is skipped.
This is useful for writing portable code that can be compiled on different platforms or under different conditions, by including or excluding code based on the environment or configuration.