Macro that prints integers without spaces: what will this program output exactly?
#include
#define PRINT(i) printf("%d,", i)
int main()
{
int x = 2, y = 3, z = 4;
PRINT(x);
PRINT(y);
PRINT(z);
return 0;
}
-
A2,3,4,
-
B2, 3, 4,
-
C2, 2, 2,
-
D3, 3, 3,
-
ECompilation error from macro
Answer
Correct Answer: 2,3,4,
Explanation
Introduction / Context:The task emphasizes exact output formatting. The macro prints an integer followed by a comma with no trailing space. When invoked three times, the outputs concatenate exactly as they are emitted, producing a compact string with commas but no spaces.
Given Data / Assumptions:
#define PRINT(i) printf("%d,", i)- Variables:
x=2,y=3,z=4. - No other characters or spaces are printed.
Concept / Approach:Each call writes the decimal value and a comma. Since the format string does not include a space (e.g., "%d, "), the characters appear back-to-back: 2, then comma, then 3, comma, then 4, comma. There is no newline unless added explicitly elsewhere.
Step-by-Step Solution:
First call: prints2,.Second call: prints 3, immediately after.Third call: prints 4, immediately after.Final console line: 2,3,4,.Verification / Alternative check:Change the macro to "%d, " and re-run; you will then see spaces: 2, 3, 4,.
Why Other Options Are Wrong:
Options with spaces do not match the exact format string.Repeated identical numbers would require passing the same variable each time.Compilation errors do not occur here.Common Pitfalls:Ignoring whitespace in format strings; assuming the console inserts spaces automatically (it does not).
Final Answer:2,3,4,.