#define MAX(a,b) ((a)>(b)?(a):(b))
Which advantage does this provide?
Macros with parentheses provide inline substitution (better performance, no function call overhead) and can work with any data type. However, they lack type safety that functions provide.
#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
Preprocessor directives (starting with #) are processed before the actual compilation by a separate preprocessor tool. They perform text substitution and conditional compilation.
#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 ADD(a,b) (a)+(b)
What is the result of: int x = ADD(3,4) * 2;
ADD(3,4)*2 expands to (3)+(4)*2 = 3+8 = 11. Wait, order of operations: 4*2 = 8, then 3+8 = 11. Actually, correct answer should be 11 based on operator precedence. Rechecking: (3)+(4)*2 following BODMAS gives 3+(8) = 11. If parentheses were ((a)+(b)), it would be (3+4)*2 = 14.
#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.
#ifdef checks if a macro is defined, #ifndef checks if it's not defined. Both are used for conditional compilation based on macro definition status.
#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)).
#define MAX 100
#define MAX 200
What will be the result?
In C, redefining a macro without #undef first will cause a compilation error. Most compilers treat macro redefinition as an error.
#allocate is not a valid preprocessor directive. Valid directives include #include, #define, #pragma, #ifdef, #ifndef, #if, #else, #elif, #endif, #error, #undef.