C Programming
C language from basics to advanced placement prep
198 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 198 questions
Q.1 Hard Preprocessor
What will happen if a macro is defined multiple times with different definitions in the same compilation unit?
#define SIZE 10
#define SIZE 20
A Compilation error: redefinition not allowed
B SIZE becomes 20 (last definition overrides)
C Warning issued; SIZE uses first definition (10)
D Preprocessor silently ignores the second definition
Correct Answer:  A. Compilation error: redefinition not allowed
EXPLANATION

Redefining a macro with a different value in the same compilation unit causes a compilation error. To redefine, you must #undef first.

Take Test
Q.2 Hard Preprocessor
Which of the following will cause infinite recursion when used?
#define RECURSE() RECURSE()
A It will cause stack overflow at runtime
B The preprocessor detects recursion and stops expansion
C It will expand infinitely, causing preprocessor to hang
D It will cause compilation error
Correct Answer:  C. It will expand infinitely, causing preprocessor to hang
EXPLANATION

The C preprocessor does not detect or prevent recursive macro expansion in its definition. RECURSE() will expand to RECURSE() infinitely, causing the preprocessor to hang or run out of memory. Modern compilers have safeguards, but theoretically this creates infinite expansion.

Take Test
Q.3 Hard Preprocessor
What will be the output of:
#define MAX(a,b) ((a)>(b)?(a):(b))
int main() { printf("%d", MAX(5++, 10)); return 0; }
A 10
B 11
C Undefined behavior
D 6
Correct Answer:  C. Undefined behavior
EXPLANATION

The macro expands to ((5++)>(10)?(5++):(10)). The variable 5++ is evaluated multiple times due to macro substitution, causing undefined behavior. Increment operations in macro arguments should be avoided. This highlights why inline functions or constexpr functions are preferred in modern C++.

Take Test
Q.4 Hard Preprocessor
What does the following preprocessor output?
#define VERSION "2024"
#define STR(x) #x
printf(STR(VERSION));
A 2024
B VERSION
C "2024"
D "VERSION"
Correct Answer:  D. "VERSION"
EXPLANATION

Stringification happens before macro expansion of arguments. STR(VERSION) becomes "VERSION" (a string literal of the token VERSION), not the value of VERSION. To stringify the expanded value, use indirect stringification with two levels of macros.

Take Test
Q.5 Hard Preprocessor
Consider: #define SWAP(a,b) {int temp=a; a=b; b=temp;}
If used in an if-else without braces, which problem occurs?
if(condition) SWAP(x,y); else printf("No swap");
A No problem, it will work correctly
B Syntax error during compilation
C The else statement becomes unreachable
D The macro cannot be used in conditional statements
Correct Answer:  C. The else statement becomes unreachable
EXPLANATION

The macro expands to {int temp=a; a=b; b=temp;}; which ends with a semicolon. This creates: if(condition) {...}; else... The semicolon after the block makes else unreachable. The macro should use do-while(0) wrapper or be wrapped in a function.

Take Test
Advertisement
Q.6 Hard Preprocessor
Which of the following will correctly print the number of arguments passed to a variadic macro in C99?
A #define COUNT_ARGS(...) sizeof((int[]){__VA_ARGS__})/sizeof(int)
B #define COUNT_ARGS(...) VA_NARGS(__VA_ARGS__)
C #define COUNT_ARGS(args...) args##__count
D #define COUNT_ARGS(...) __VA_ARGS__##_count
Correct Answer:  A. #define COUNT_ARGS(...) sizeof((int[]){__VA_ARGS__})/sizeof(int)
EXPLANATION

C99 variadic macros don't have a built-in way to count arguments. Option A uses a clever technique with array size calculation. Option B references non-standard VA_NARGS. Options C and D use invalid syntax with token pasting.

Take Test
Q.7 Hard Preprocessor
Consider the macro: #define AREA(r) 3.14*r*r
What is the issue and how to fix it?
A No issue, it works correctly
B Issue: missing parentheses; Fix: #define AREA(r) (3.14*(r)*(r))
C Issue: type mismatch; Fix: use long double
D Issue: cannot use floats in macros
Correct Answer:  B. Issue: missing parentheses; Fix: #define AREA(r) (3.14*(r)*(r))
EXPLANATION

Without parentheses, AREA(2+1) becomes 3.14*2+1*2+1 = 11.28. Proper version: #define AREA(r) (3.14*((r))*((r))) protects argument evaluation.

Take Test
Q.8 Hard Preprocessor
What will happen if we define a macro with the same name as a C standard library function?
A Compilation error
B The macro replaces the function everywhere
C Runtime error
D No effect, function takes precedence
Correct Answer:  B. The macro replaces the function everywhere
EXPLANATION

Macros are textual replacements that happen before compilation. If a macro has the same name as a library function, the macro replaces all occurrences of that name.

Take Test
Q.9 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.10 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