Govt. Exams
Entrance Exams
While #pragma once is technically non-standard, it is supported by virtually all modern compilers (GCC, Clang, MSVC) since 2024. It's simpler than include guards and prevents multiple inclusion more elegantly. Both approaches work, but #pragma once is increasingly preferred.
#line directive changes the line number and filename reported by the compiler in error messages and by __LINE__ and __FILE__ macros. Syntax: #line line_number "filename"
#define PRINT(x) printf(#x)
int main() { int a = 10; PRINT(a); return 0; }
The '#' operator (stringification) converts the macro argument into a string literal. PRINT(a) expands to printf("a"), which prints 'a'. If it were PRINT(10), it would print '10' as a string.
#define SQUARE(x) x*x
int main() { int a = 5; printf("%d", SQUARE(a+1)); return 0; }
Without parentheses around 'x' in the macro, SQUARE(a+1) expands to a+1*a+1 = 5+1*5+1 = 5+5+1 = 11. The macro should be defined as #define SQUARE(x) ((x)*(x)) to avoid operator precedence issues.
Macros are expanded during preprocessing in each translation unit separately. If #define MAX 100 appears in multiple source files, the constant 100 gets substituted in each file independently, potentially creating code duplication. To avoid this, the macro should be defined in a header file with include guards.
Backslash (\) at the end of line continues a macro definition to the next line. This is the standard way to create multi-line macros in C.
Preprocessor is a separate tool that processes directives before actual compilation. It prevents recursive macro expansion to avoid infinite loops.
#define MIN(a,b) ((a)
MIN(10, 5*2) expands to ((10)<(5*2)?(10):(5*2)) = ((10)<(10)?10:10) = 10. Both values are equal due to evaluation order.
#define ADD(x,y) (x+y)
int main() { printf("%d", ADD(5,3)*2); return 0; }
ADD(5,3)*2 expands to (5+3)*2 = 8*2 = 16. Parentheses around the entire macro definition protect against precedence issues.
#pragma pack() controls how compiler aligns structure members in memory. #pragma pack(1) minimizes padding, while #pragma pack() without arguments resets to default.