Govt. Exams
Entrance Exams
int x = MAX(2, 3); int y = MAX(++i, ++j);
In MAX(++i, ++j), both i and j are incremented twice due to double evaluation. Parentheses help but don't solve side effects completely.
#define STR(x) #x
int main() { printf("%s", STR(Hello)); return 0; }
The # operator converts the macro argument into a string literal. STR(Hello) becomes "Hello", and printf outputs Hello without quotes.
Option C using do-while(0) is the safest approach to avoid issues with semicolons in if-else statements. Option B can cause problems with control flow statements.
#define SQUARE(x) x*x
int main() { int a = SQUARE(2+3); printf("%d", a); return 0; }
SQUARE(2+3) expands to 2+3*2+3 = 2+6+3 = 11 due to operator precedence. Proper macro should use parentheses: #define SQUARE(x) ((x)*(x))
__LINE__ expands to the current line number and __FILE__ expands to the current filename. These are predefined macros that provide compile-time information useful for debugging and error reporting.
#define SIZE 10
int arr[SIZE];
#undef SIZE
int arr2[SIZE];
After #undef SIZE, the macro SIZE is no longer defined. Attempting to use SIZE in int arr2[SIZE] will cause a compilation error because SIZE is not recognized by the compiler.
#define MAX(a,b) (a>b?a:b)
int x = MAX(MAX(2,5), MAX(3,4));
Inner MAX calls: MAX(2,5)=5 and MAX(3,4)=4. Outer MAX call: MAX(5,4)=5. The macro correctly handles nested calls due to proper parenthesization.
#define ABS(x) ((x)
The macro correctly uses the ternary operator with proper parenthesization. Each use of x is wrapped in parentheses to prevent precedence issues. This is a well-formed macro for computing absolute value.
The ## operator (token pasting) concatenates two tokens into a single token during preprocessing. For example, #define CONCAT(a,b) a##b creates CONCAT(var,1) as var1.
#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.