Showing 31–40 of 1,000 questions
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.
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.
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.
What is the output of:
#define MIN(a,b) ((a)
A
5
B
10
C
15
D
Compilation error
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.
What does #undef directive do?
A
Undefines a previously defined macro
B
Checks if macro is undefined
C
Includes undefined symbols
D
Marks variables as undefined
Correct Answer:
A. Undefines a previously defined macro
EXPLANATION
#undef cancels the definition of a macro, allowing it to be redefined later. After #undef, the macro name becomes undefined.
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
EXPLANATION
ADD(5,3)*2 expands to (5+3)*2 = 8*2 = 16. Parentheses around the entire macro definition protect against precedence issues.
Which header file contains the definition of NULL macro?
Correct Answer:
C. Both A and B
EXPLANATION
NULL is defined in multiple headers including <stdio.h>, <stdlib.h>, <stddef.h>, and others. Any of these can be included to use NULL.
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.
What is the issue with this macro: #define MAX(a,b) a>b?a:b
int x = MAX(2, 3); int y = MAX(++i, ++j);
A
Syntax error
B
Arguments are evaluated multiple times
C
No issue
D
Type mismatch
Correct Answer:
B. Arguments are evaluated multiple times
EXPLANATION
In MAX(++i, ++j), both i and j are incremented twice due to double evaluation. Parentheses help but don't solve side effects completely.
Which predefined macro gives the line number in the source file?
A
__FILE__
B
__LINE__
C
__DATE__
D
__STDC__
Correct Answer:
B. __LINE__
EXPLANATION
__LINE__ expands to the current line number. __FILE__ gives filename, __DATE__ gives compilation date, __STDC__ indicates C standard compliance.