String literals and macros: what does this program print?\n\n#include<stdio.h>\n#define MESS junk\n\nint main()\n{\n printf("MESS\n");\n return 0;\n}\n\nDoes the macro affect the content of the string literal?

Difficulty: Easy

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\n"); unchanged.At runtime, printf prints the literal text MESS followed by a newline.



Verification / Alternative check:
If the code were printf("%s\n", 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

More Questions from C Preprocessor

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion