Govt Exams
#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.
#define is used to create macro definitions and constants at compile time. #const, #constant, and #fixed are not valid preprocessor directives in C.
#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.
#define OFFSET(type, field) ((size_t)&(((type *)0)->field))
This macro calculates the byte offset of a field within a structure by casting 0 as a pointer to the type and taking the address of the field. Similar to the offsetof() macro in stddef.h.
Standard library headers like stdio.h, stdlib.h, string.h are included using angle brackets #include <filename>.
#define MIN(x,y) ((x)
The macro expands to ((a++)<(b++)?(a++):(b++)). Since 5<3 is false, b++ is evaluated twice (once in comparison, once in ternary), making b=5. a is incremented once in comparison, so a=6.
Both #ifndef and #if !defined(MACRO) are equivalent and check if a macro is not defined. #ifndef is simpler syntax for this specific case.
#define PRINT(x) printf(#x " = %d\n", x)
PRINT(5+3);
The # operator converts 5+3 into the string "5+3". The macro expands to printf("5+3" " = %d\n", 5+3) which prints: 5+3 = 8
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.