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.
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.
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.
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.
Which of the following macro definitions will correctly compute the absolute value?
#define ABS(x) ((x)
ACorrect - uses ternary operator with proper precedence
BIncorrect - missing parentheses around x
CIncorrect - division cannot be used
DCorrect but inefficient
Correct Answer:
A. Correct - uses ternary operator with proper precedence
EXPLANATION
The macro correctly uses the ternary operator with proper parenthesization. Each use of x is wrapped in parentheses to prevent precedence issues. This is a well-formed macro for computing absolute value.
What is the token pasting operator (##) used for in C preprocessor?
ACreates comments
BConcatenates two tokens into a single token
CPerforms mathematical addition
DIncludes header files
Correct Answer:
B. Concatenates two tokens into a single token
EXPLANATION
The ## operator (token pasting) concatenates two tokens into a single token during preprocessing. For example, #define CONCAT(a,b) a##b creates CONCAT(var,1) as var1.