#define STRINGIFY(x) #x
The # operator (stringification) converts its argument into a string literal. For example, STRINGIFY(hello) becomes "hello". This is useful for creating string representations of identifiers.
#define ADD(a,b) a+b
int result = ADD(2,3)*2;
ADD(2,3)*2 expands to 2+3*2 which equals 2+6=8 due to operator precedence. Wait - it's (2+3)*2 conceptually but expands as 2+3*2 = 2+6 = 8. Actually, the macro expands as: 2+3*2 = 8. The answer should be 8. Let me recalculate: 2+3*2 follows standard precedence where * is evaluated first: 2+(3*2)=2+6=8.
#define SQUARE(x) x*x
int a = SQUARE(5+3);
SQUARE(5+3) expands to 5+3*5+3 due to operator precedence (multiplication before addition), which equals 5+15+3=23. However, the expression is actually 5+3*5+3=23. Actually it's (5+3)*(5+3)=64 if properly parenthesized. Without parentheses: 5+3*5+3 = 5+15+3 = 23. But SQUARE(5+3) = 5+3*5+3 = 23. Re-evaluating: x*x where x=5+3 becomes 5+3*5+3 = 23+21 = 44.
#ifndef guards and #pragma once are both used to prevent multiple inclusion. #pragma once is non-standard but widely supported, while #ifndef is the standard approach.
Option B is safest as all operands and the entire expression are parenthesized, preventing operator precedence issues and side effects.
#undef removes a macro definition from the preprocessor's symbol table, allowing you to redefine it later without errors.
#define STR(x) #x
printf("%s", STR(HELLO));
The # operator (stringification) converts the macro argument into a string literal. STR(HELLO) becomes "HELLO", and printf prints: HELLO
#define CONCATENATE(a,b) a##b
The ## operator (token pasting operator) concatenates two tokens into a single token during preprocessing. For example, CONCATENATE(var,1) becomes var1.
#define DEBUG 1
#if DEBUG
printf("Debug mode ON");
#else
printf("Debug mode OFF");
#endif
Since DEBUG is defined as 1 (non-zero), the #if DEBUG condition is true, so only the code in #if block executes. The #else block is skipped.
#define SQUARE(x) x*x
int result = SQUARE(2+3);
printf("%d", result);
SQUARE(2+3) expands to 2+3*2+3 = 2+6+3 = 11 due to operator precedence. Macros don't evaluate arguments; they substitute text directly. Safe macro should be ((x)*(x)).