Which of the following correctly uses #pragma once for header file protection in 2024 standards?
A#pragma once is non-standard and should be avoided
B#pragma once is widely supported by modern compilers and is preferred over include guards
C#pragma once and include guards provide identical functionality
D#pragma once can only be used with GCC compiler
Correct Answer:
B. #pragma once is widely supported by modern compilers and is preferred over include guards
EXPLANATION
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.
What is the purpose of #line directive in C preprocessing?
ATo include a specific line from another file
BTo specify the filename and line number for error reporting
CTo create a line continuation in macro definition
DTo mark the end of a preprocessor block
Correct Answer:
B. To specify the filename and line number for error reporting
EXPLANATION
#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"
What will be the result of executing the following?
#define PRINT(x) printf(#x)
int main() { int a = 10; PRINT(a); return 0; }
Aa
B10
CCompilation error
Da10
Correct Answer:
A. a
EXPLANATION
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.
What is the output of the following code?
#define SQUARE(x) x*x
int main() { int a = 5; printf("%d", SQUARE(a+1)); return 0; }
A25
B36
C11
D30
Correct Answer:
C. 11
EXPLANATION
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.
Consider the preprocessor directive #define MAX 100. If this macro is used in multiple source files, which statement is TRUE?
AThe macro definition will be duplicated in each object file, increasing binary size
BThe macro is globally shared across all files without duplication
CIt will cause a linker error due to multiple definitions
DThe macro will only work in the file where it is defined
Correct Answer:
A. The macro definition will be duplicated in each object file, increasing binary size
EXPLANATION
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.
What is the difference between #include and #include "stdio.h"?
ANo difference, both are same
B searches standard directories; "" searches current directory first
C"" is for user-defined headers; is for system headers
DBoth B and C
Correct Answer:
D. Both B and C
EXPLANATION
Angle brackets <> search system/standard directories. Quotes "" search current directory first, then standard directories. Used for custom headers vs system headers respectively.