C#define PRINT(x) do { printf("%d", x); printf("\n"); } while(0)
DBoth 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.
Consider the following macro:
#define SWAP(a,b) {int temp=a; a=b; b=temp;}
What issue might occur with this macro?
AWorks perfectly for all data types
BOnly works for integers, fails for other types like float
CRequires variables to be initialized before use
DThe 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.
What is the purpose of predefined macros like __LINE__ and __FILE__?
ATo store user-defined variables
BTo provide compile-time information about the source code location
CTo create inline functions
DTo 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.
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.
What will happen when this code is compiled?
#define SIZE 10
int arr[SIZE];
#undef SIZE
int arr2[SIZE];
ACompilation error - SIZE is undefined
BNo error - both arrays created with size 10
CWarning only - arr2 gets default size
DRuntime 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.
BA macro whose definition contains unmatched parentheses
CA macro that is defined but later undefined without being used properly
DA 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.