C Test Incorrectly Uses printf() - Please Confirm
- From: "Martin O'Brien" <martinobr@xxxxxxxxx>
- Date: Sun, 8 Aug 2010 11:55:26 -0700 (PDT)
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?
--
Martin
.
- Follow-Ups:
- Re: C Test Incorrectly Uses printf() - Please Confirm
- From: Denis McMahon
- Re: C Test Incorrectly Uses printf() - Please Confirm
- From: spinoza1111
- Re: C Test Incorrectly Uses printf() - Please Confirm
- From: Kenneth Brody
- Re: C Test Incorrectly Uses printf() - Please Confirm
- From: pete
- Re: C Test Incorrectly Uses printf() - Please Confirm
- From: Barry Schwarz
- Re: C Test Incorrectly Uses printf() - Please Confirm
- From: Eric Sosman
- Re: C Test Incorrectly Uses printf() - Please Confirm
- From: Ersek, Laszlo
- Re: C Test Incorrectly Uses printf() - Please Confirm
- From: bert
- Re: C Test Incorrectly Uses printf() - Please Confirm
- From: Shao Miller
- Re: C Test Incorrectly Uses printf() - Please Confirm
- From: Malcolm McLean
- Re: C Test Incorrectly Uses printf() - Please Confirm
- Prev by Date: Re: print alphabet
- Next by Date: Re: C Test Incorrectly Uses printf() - Please Confirm
- Previous by thread: int volati;
- Next by thread: Re: C Test Incorrectly Uses printf() - Please Confirm
- Index(es):
Relevant Pages
|