Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

490 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 490
Topics in C Programming
Q.11 Medium Preprocessor
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.

Test
Q.12 Medium Preprocessor
What is the purpose of #line directive in C preprocessing?
A To include a specific line from another file
B To specify the filename and line number for error reporting
C To create a line continuation in macro definition
D To 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"

Test
Q.13 Medium Preprocessor
What will be the result of executing the following?
#define PRINT(x) printf(#x)
int main() { int a = 10; PRINT(a); return 0; }
A a
B 10
C Compilation error
D a10
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.

Test
Q.14 Medium Preprocessor
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; }
A 25
B 36
C 11
D 30
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.

Test
Q.15 Medium Preprocessor
Consider the preprocessor directive #define MAX 100. If this macro is used in multiple source files, which statement is TRUE?
A The macro definition will be duplicated in each object file, increasing binary size
B The macro is globally shared across all files without duplication
C It will cause a linker error due to multiple definitions
D The 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.

Test
Q.16 Medium Preprocessor
What is the correct way to define a multi-line macro in C?
A #define MULTI printf("Line1"); printf("Line2");
B #define MULTI printf("Line1") \ printf("Line2")
C Macros cannot span multiple lines
D #define MULTI { printf("Line1"); printf("Line2"); }
Correct Answer:  B. #define MULTI printf("Line1") \ printf("Line2")
EXPLANATION

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.

Test
Q.17 Medium Preprocessor
Which of the following about preprocessor is TRUE?
A Preprocessor is part of C compiler
B Preprocessor processes code before compilation
C Preprocessor cannot handle recursive macros
D Both B and C
Correct Answer:  D. Both B and C
EXPLANATION

Preprocessor is a separate tool that processes directives before actual compilation. It prevents recursive macro expansion to avoid infinite loops.

Test
Q.18 Medium Preprocessor
What is the output of:
#define MIN(a,b) ((a)
A 5
B 10
C 15
D Compilation error
Correct Answer:  B. 10
EXPLANATION

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.

Test
Q.19 Medium Preprocessor
What is the output of:
#define ADD(x,y) (x+y)
int main() { printf("%d", ADD(5,3)*2); return 0; }
A 16
B 13
C Compilation error
D 23
Correct Answer:  A. 16
EXPLANATION

ADD(5,3)*2 expands to (5+3)*2 = 8*2 = 16. Parentheses around the entire macro definition protect against precedence issues.

Test
Q.20 Medium Preprocessor
What is the purpose of #pragma pack() directive?
A To define a macro
B To control structure member alignment
C To include header files
D To create string macros
Correct Answer:  B. To control structure member alignment
EXPLANATION

#pragma pack() controls how compiler aligns structure members in memory. #pragma pack(1) minimizes padding, while #pragma pack() without arguments resets to default.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips