example4_macros_define.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

// Simple constant macro
#define MAX_SIZE 100

// Function-like macro with multiple statements
#define PRINT_AND_DOUBLE(x) do { \
    std::cout << "Original value: " << x << std::endl; \
    x *= 2; \
    std::cout << "Doubled value: " << x << std::endl; \
} while(0)

// Macro with stringification
#define STRINGIFY(x) #x

// Macro with token concatenation
#define CONCAT(a, b) a ## b

int main() {
    int array[MAX_SIZE];
    std::cout << "Array size: " << MAX_SIZE << std::endl;

    int value = 5;
    PRINT_AND_DOUBLE(value);

    std::cout << STRINGIFY(Hello World) << std::endl;

    int CONCAT(num, 1) = 10;
    int CONCAT(num, 2) = 20;
    std::cout << "num1: " << num1 << ", num2: " << num2 << std::endl;

    return 0;
}
Back to preprocessing_directives