Re: question about printf

From: Richard Bos (rlb_at_hoekstra-uitgeverij.nl)
Date: 05/26/04


Date: Wed, 26 May 2004 12:26:51 GMT

Case <no@no.no> wrote:

> Richard Bos wrote:
> > "Vijay Kumar R Zanvar" <vijoeyz@globaledgesoft.com> wrote:
> >
> >>"pratik" <pratikthakkar007@yahoo.com> wrote in message
> >>
> >>> printf("%d%d%d",a++,++a,a);

> >> This program can said to be unportable. The reason is that the order
> >>of evaluation of arguments is unspecified. The above program, upon compilation,
> >>gives the following warning messages.
> >
> > No, no, and no. It is not merely unportable; it is downright incorrect.
> > The reason is not merely that argument evaluation order is unspecified,
> > but that a is modified twice, and read one extra time, between adjacent
> > sequence points, causing undefined, not just unspecified, behaviour. And
> > it may give these messages on _your_ system, but that is far from
> > guaranteed.
>
> Which two sequence points do you mean? I've read the FAQ about this
> issue and if I understand it right, there are no sequence points in
> a function argument list.

Exactly. The previous sequence point is just after the previous
statement (or in this case, declaration); the next sequence point is
just before printf() is called. Those are the two adjacent sequence
points I meant; between them, a is modified twice and read three times,
of which once not for the reason of computing the modified value.

Richard



Relevant Pages

  • Re: order of evaluation
    ... > The compiler is allowed to determine the order in which the 4 calls ... but it may not execute them concurrently (unless you cannot detect ... the only thing unspecified is the order of sequence points. ... No variable is modified twice between sequence points. ...
    (comp.lang.cpp)
  • Re: "Variables" tutorial available (Windows, mingw/msvc)
    ... >>I'm afraid it is undefined behaviour, since x is modified twice without ... >>about unspecified order of evaluation. ... > constraint (sequence point) for the expression in which it appears. ... Therefore my example can _never_ invoke ...
    (comp.lang.cpp)
  • Re: Is i = i++; bad style?
    ... "Between the previous and next sequence point an object shall have its ... Assignment does not introduce a sequence point so i is modified twice ... constraint causes undefined behavior. ...
    (comp.lang.c)
  • Re: 2 doubts !
    ... i is modified twice without intervening sequence ... See FAQ section 3.2. ...
    (comp.lang.c)
  • Re: coin toss problem
    ... no. of 6 heads in a row in a sequence of 200 tosses was at least one... ... to global variables in a program this small and simple. ... There is no good reason here. ...
    (comp.lang.c)