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 #endifThis 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?
✗ Incorrect
#ifndef means "if not defined" and includes code only if the macro is not defined.What happens to code inside
#ifdef MACRO if MACRO is not defined?✗ Incorrect
If the macro is not defined, the code inside
#ifdef is skipped.Which directive can be used to provide an alternative code block if a condition is false?
✗ Incorrect
#else is used to specify code that compiles when the #if or #ifdef condition is false.What is the role of
#endif in conditional compilation?✗ Incorrect
#endif marks the end of a conditional compilation block started by #if, #ifdef, or #ifndef.Which directive would you use to check a condition with a value, not just if a macro is defined?
✗ Incorrect
#if allows checking expressions or values, unlike #ifdef which only checks if a macro is defined.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.