Conditional Compilation

Conditional Compilation

Use the defined operator to conditionally compile a piece of code based on whether a DEBUG macro is defined. This code should use dynamic macros such as __LINE__ , __FILE__ , __DATE__ , and __TIME__ to print useful debug information.

Answer

#include <iostream>

// Define the DEBUG macro to enable debug information
#define DEBUG

int main() {
    // Conditionally compile this block if DEBUG is defined
    #if defined(DEBUG)
    std::cout << "Debug Information:\n";
    std::cout << "File: " << __FILE__ << "\n";
    std::cout << "Line: " << __LINE__ << "\n";
    std::cout << "Compiled on: " << __DATE__ << " at " << __TIME__ << "\n";
    std::cout << "End of Debug Information\n\n";
    #endif

    std::cout << "Program is running normally." << std::endl;

    return 0;
}

Experiment by commenting out the #define DEBUG line and observe how the output changes.

Questions