M
Martin O'Brien
I took an online C test a few months ago. I actually thought the test
was better than some I've taken, but one question in particular I
think has the wrong answer. The question is this:
What is printed:
int a = 1;
printf("%d", ++a, a + 5);
a. 1
b. 2
c. 7
d. undefined
I selected d. This is the explanation given as to why b is the correct
answer.
The first expression in the parameter list following the format
string is paired with the first (and only) conversion specification.
The increment is a prefix so the result of the operation is the a + 1.
Since there are more items in the value list than there are conversion
specifications, the extra value is not shown.
I believe the correct answer is d because according to K&R2 (and by
implication the Standard) the order in which function arguments are
evaluated is not defined; in fact, K&R2's example, in Section 2.12,
shows the variable n being used twice in the same printf call (albeit
with the correct number of conversion specifications).
Am I correct that d is the correct answer?
was better than some I've taken, but one question in particular I
think has the wrong answer. The question is this:
What is printed:
int a = 1;
printf("%d", ++a, a + 5);
a. 1
b. 2
c. 7
d. undefined
I selected d. This is the explanation given as to why b is the correct
answer.
The first expression in the parameter list following the format
string is paired with the first (and only) conversion specification.
The increment is a prefix so the result of the operation is the a + 1.
Since there are more items in the value list than there are conversion
specifications, the extra value is not shown.
I believe the correct answer is d because according to K&R2 (and by
implication the Standard) the order in which function arguments are
evaluated is not defined; in fact, K&R2's example, in Section 2.12,
shows the variable n being used twice in the same printf call (albeit
with the correct number of conversion specifications).
Am I correct that d is the correct answer?