Govt Exams
When a macro is redefined, the new definition replaces the old one. Some compilers may issue a warning about redefinition.
#define MAX(a,b) (a>b?a:b)
printf("%d", MAX(10, 5));
The macro expands to (10>5?10:5) which evaluates to 10 since the condition is true.
Angle brackets search in standard system directories. Double quotes search in the current directory first, then standard directories. This is the standard convention.
Preprocessor directives can appear in any order in the source code. However, logically, #include is often used before #define and #ifdef checks.
#ifdef checks if a macro identifier is currently defined. Code following it is compiled only if the macro is defined.
#define ADD(a,b) a+b
int result = ADD(5,3)*2;
The macro expands to 5+3*2 = 5+6 = 11, not 16. However, due to operator precedence, it becomes 5+3*2. Actually, it expands correctly to (5+3)*2 = 16 when evaluated.
#define SQUARE(x) ((x)*(x))
This is a function-like macro that takes parameter x and expands to ((x)*(x)). The parentheses around x prevent operator precedence issues.
#include <filename> is used for standard library files (searched in standard paths). #include "filename" is used for user-defined header files (searched in current directory first).
#define MAX 5
int arr[MAX];
The preprocessor replaces MAX with 5, creating an array of size 5. This is a valid use of macro expansion.
#declare is not a valid preprocessor directive. Valid directives include #define, #include, #pragma, #ifdef, #endif, #if, #else, etc.