0
0
Cprogramming~5 mins

Conditional compilation - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is conditional compilation in C?
Conditional compilation allows the compiler to include or exclude parts of the code based on certain conditions, usually using preprocessor directives like #if, #ifdef, and #ifndef.
Click to reveal answer
beginner
What does the #ifdef directive do?
#ifdef checks if a macro is defined. If it is, the code following it up to #endif is included in compilation.
Click to reveal answer
beginner
How do you exclude code when a macro is defined?
Use #ifndef MACRO_NAME to include code only if the macro is NOT defined.
Click to reveal answer
beginner
What is the purpose of #else in conditional compilation?
#else provides an alternative block of code to compile if the condition in #if or #ifdef is false.
Click to reveal answer
intermediate
Give an example of conditional compilation to compile code only on Windows.
Example:<br>
#ifdef _WIN32
// Windows-specific code
#endif
This compiles the code inside only if _WIN32 is defined, which usually means the program is being compiled on Windows.
Click to reveal answer
Which directive checks if a macro is NOT defined?
A#else
B#ifndef
C#if
D#ifdef
What happens to code inside #ifdef MACRO if MACRO is not defined?
AIt is excluded from compilation
BIt is always compiled
CIt causes a compilation error
DIt is compiled twice
Which directive can be used to provide an alternative code block if a condition is false?
A#include
B#endif
C#define
D#else
What is the role of #endif in conditional compilation?
AIt includes a file
BIt defines a macro
CIt ends a conditional directive block
DIt comments out code
Which directive would you use to check a condition with a value, not just if a macro is defined?
A#if
B#ifdef
C#ifndef
D#else
Explain how conditional compilation helps in writing cross-platform C programs.
Think about how you can write code that works on Windows and Linux by including only the right parts.
You got /3 concepts.
    Describe the difference between #ifdef and #if directives.
    One checks presence, the other checks a condition.
    You got /3 concepts.