C macros with stringification: what does this program print?
#include
#define JOIN(s1, s2) printf("%s=%s %s=%s
", #s1, s1, #s2, s2)
int main()
{
char *str1 = "India";
char *str2 = "CURIOUSTAB";
JOIN(str1, str2);
return 0;
}
-
Astr1=India str2=CURIOUSTAB
-
Bstr1=CuriousTab str2=CURIOUSTAB
-
Cstr1=India str2=CuriousTab
-
DError during macro substitution
-
Estr1=str1 str2=str2
Answer
Correct Answer: str1=India str2=CURIOUSTAB
Explanation
Introduction / Context:This macro uses the C preprocessor’s stringification operator # to print both the parameter names and their string values. Understanding # helps in building debugging and logging helpers that display variable names with their contents.
Given Data / Assumptions:
#define JOIN(s1, s2) printf("%s=%s %s=%s", #s1, s1, #s2, s2)str1 = "India",str2 = "CURIOUSTAB"- Valid C compilation environment.
Concept / Approach:The stringification operator # converts the macro argument token sequence into a string literal. Hence #s1 becomes "str1" and #s2 becomes "str2". The other arguments (without #) pass the variable values, which are pointers to the respective strings. The final output is the variable names and their stored strings.
Step-by-Step Solution:
#s1 → "str1", s1 → "India".#s2 → "str2", s2 → "CURIOUSTAB".Printed line: str1=India str2=CURIOUSTAB.Verification / Alternative check:Change str1 to another variable name (e.g., country) and observe that the left-hand label changes accordingly.
Why Other Options Are Wrong:
Variants with “CuriousTab” change the actual string contents, which the code does not do.“Error” is incorrect: the macro is valid and common practice.Common Pitfalls:Assuming # stringifies the value instead of the token; swapping names and values.
Final Answer:str1=India str2=CURIOUSTAB.