Home Subjects C Programming Preprocessor

C Programming
Preprocessor

C language from basics to advanced placement prep

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 41–50 of 100
Topics in C Programming
Q.41 Medium Preprocessor
What is the output of:
#define STR(x) #x
int main() { printf("%s", STR(Hello)); return 0; }
A Hello
B "Hello"
C x
D Compilation error
Correct Answer:  A. Hello
EXPLANATION

The # operator converts the macro argument into a string literal. STR(Hello) becomes "Hello", and printf outputs Hello without quotes.

Take Test
Q.42 Easy Preprocessor
What does the ## operator in preprocessor do?
A Stringification
B Token pasting
C Comparison
D Bitwise AND
Correct Answer:  B. Token pasting
EXPLANATION

The ## operator concatenates two tokens into one. For example, #define CONCAT(a,b) a##b creates CONCAT(hello, world) → helloworld.

Take Test
Q.43 Medium Preprocessor
Which of the following correctly defines a macro with multiple statements?
A #define PRINT(x) printf("%d", x); printf("\n");
B #define PRINT(x) { printf("%d", x); printf("\n"); }
C #define PRINT(x) do { printf("%d", x); printf("\n"); } while(0)
D Both B and C
Correct Answer:  C. #define PRINT(x) do { printf("%d", x); printf("\n"); } while(0)
EXPLANATION

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.

Take Test
Q.44 Medium Preprocessor
What is the output of:
#define SQUARE(x) x*x
int main() { int a = SQUARE(2+3); printf("%d", a); return 0; }
A 25
B 11
C 15
D Compilation error
Correct Answer:  B. 11
EXPLANATION

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))

Take Test
Q.45 Easy Preprocessor
What is the output of the following code?
#define PI 3.14
int main() { printf("%f", PI); return 0; }
A 3.14
B Compilation error
C 3.140000
D Undefined behavior
Correct Answer:  C. 3.140000
EXPLANATION

PI is replaced by 3.14 during preprocessing. When printed with %f format specifier, it displays as 3.140000 (default 6 decimal places).

Take Test
Q.46 Hard Preprocessor
Consider the following macro:
#define SWAP(a,b) {int temp=a; a=b; b=temp;}
What issue might occur with this macro?
A Works perfectly for all data types
B Only works for integers, fails for other types like float
C Requires variables to be initialized before use
D The curly braces prevent use inside if-else statements without care
Correct Answer:  D. The curly braces prevent use inside if-else statements without care
EXPLANATION

While the macro works for all types (a and b are copied), the curly braces can cause issues when used in if-else statements without proper syntax (like missing semicolon after if). Additionally, it's limited to specific types. The safest answer is D regarding syntactic issues with placement.

Take Test
Q.47 Medium Preprocessor
What is the purpose of predefined macros like __LINE__ and __FILE__?
A To store user-defined variables
B To provide compile-time information about the source code location
C To create inline functions
D To define character arrays
Correct Answer:  B. To provide compile-time information about the source code location
EXPLANATION

__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.

Take Test
Q.48 Easy Preprocessor
Which of the following correctly demonstrates the use of conditional compilation?
A #ifdef DEBUG printf("Debug mode"); #endif
B #if DEBUG printf("Debug mode"); #endif
C #ifelse DEBUG printf("Debug mode"); #endif
D #iftrue DEBUG
Correct Answer:  A. #ifdef DEBUG printf("Debug mode"); #endif
EXPLANATION

Option A uses #ifdef to check if DEBUG is defined, which is correct for conditional compilation. Option B would check if DEBUG has a non-zero value (expression-based). Options C and D have invalid syntax.

Take Test
Q.49 Medium Preprocessor
What will happen when this code is compiled?
#define SIZE 10
int arr[SIZE];
#undef SIZE
int arr2[SIZE];
A Compilation error - SIZE is undefined
B No error - both arrays created with size 10
C Warning only - arr2 gets default size
D Runtime error
Correct Answer:  A. Compilation error - SIZE is undefined
EXPLANATION

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.

Take Test
Q.50 Hard Preprocessor
What is a dangling macro problem in C?
A A macro that is never used in the program
B A macro whose definition contains unmatched parentheses
C A macro that is defined but later undefined without being used properly
D A macro that causes infinite recursion during preprocessing
Correct Answer:  C. A macro that is defined but later undefined without being used properly
EXPLANATION

A dangling macro typically refers to a macro that is defined but its usage may cause issues. More accurately, it's when a macro's scope or definition is unclear, especially in conditional compilation scenarios where #undef might cause problems.

Take Test
IGET
iget AI
Online · Ask anything about exams
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