String literals and macros: what does this program print?
#include
#define MESS junk
int main()
{
printf("MESS
");
return 0;
}
Does the macro affect the content of the string literal?
-
Ajunk
-
BMESS
-
CError
-
DNothing will print
-
ENone of the above
Answer
Correct Answer: MESS
Explanation
Introduction / Context:Preprocessor macros perform textual substitution before compilation, but they do not modify the contents of string literals. This question verifies that understanding.
Given Data / Assumptions:
- Macro definition: #define MESS junk.
- printf is called with a string literal "MESS".
- No macro is used outside the quotes in the call to printf.
Concept / Approach:During preprocessing, identifiers inside string literals are not expanded. Only tokens outside string literals are candidates for macro substitution.
Step-by-Step Solution:Preprocessor sees printf("MESS"); unchanged.At runtime, printf prints the literal text MESS followed by a newline.
Verification / Alternative check:If the code were printf("%s", MESS); without quotes around MESS, then the token MESS would expand to junk and compilation would likely fail (undefined identifier) unless junk were defined as a string.
Why Other Options Are Wrong:"junk": would require macro expansion inside the string literal, which does not occur.Error / nothing: the code is valid and prints text.
Common Pitfalls:Expecting the preprocessor to parse inside quotes; forgetting that stringization requires the # operator within a macro definition.
Final Answer:MESS