Re: printf execution problem



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

rahul8143@xxxxxxxxx wrote:
> Does printf output depends on compilers or operating systems? I have
> one program
> int main()
> {
> int i=5;
> printf("%d %d %d",i++,i++,i++);
> return 0;
> }
> Output of this program on DOS (TC) 7 6 5
> Output of this program on Windows (VC++) 5 5 5
> Output of this program on Linux (GCC) 7 6 5
> so these outputs conlude that behaviour of this program is undefined.
> is that right?

Right. And the output can be /anything/ (although, numbers are
reasonable for undefined behaviour)

> but can anybody have any other answer to this behaviour in terms of OS
> or Compiler???

The values can be explained by compiler behaviour outside the scope of
the C standard.

Consider this: the C standard makes no specification of the order in
which function arguments are evaluated, and leaves the order to the
compiler implementor. Typical orders are right-to-left, and
left-to-right, but other orders are possible.

Right-to-left order would take an argument list of fn(a,b,c), and
evaluate the c argument before the b argument, and
evaluate the b argument before the a argument

Left-to-right order would take an argument list of fn(a,b,c) and
evaluate the a argument before the b argument, and
evaluate the b argument before the c argument

Also, the C standard makes no guarantees of /when/ changes will be made,
other than that changes will be guaranteed to be made at "sequence
points" in the code. The compiler is free to make some changes prior to
the sequence point; the programmer is restricted to assuming that such
changes haven't actually happened until the sequence point.

Taken together, these two points (order evaluation and execution) can
explain your results. Keep in mind, this is guesswork, and is completely
outside the realm of the C standard.

So, let's guess...

For your TC and GCC results
> Output of this program on DOS (TC) 7 6 5
> Output of this program on Linux (GCC) 7 6 5
it appears that
a) each compiler evaluates function arguments right to left, and
b) each compiler makes changes at the comma that separates function
arguments
which makes the leftmost argument 5 (post incremented to 6),
the middle argument 6 (post incremented to 7), and
the rightmost argument 7 (post incremented to 8)

OTOH, for your VC++ results,
> Output of this program on Windows (VC++) 5 5 5
it appears that the compiler makes changes after all the arguments have
been parsed, and there is no hint at the order of evaluation of the
arguments.

And, that is why dependance on undefined behaviour is not a good idea.


> Thanks for help.
>


- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDPU9TagVFX4UWr64RAjTrAKDs0QpHcH5dlrHsu43M0wxo+DSP1gCfXx9d
tTPMaN0noTDWtHDy7a+303M=
=oByo
-----END PGP SIGNATURE-----
.



Relevant Pages

  • Re: pointers?
    ... int foo ... If the compiler takes the effort to actually recognize that this is ... addresses do not work like in a flat address space. ... "contiguous region" in the standard document. ...
    (comp.lang.forth)
  • Re: Unknown function
    ... int main ... message) "the compiler actually dos not "know" the signature of malloc()", I understand it in the way that malloc is not known to the compiler at that specific point of program. ... What the standard mandates that the compiler must do and what the compiler can do in addition to what the standard mandates. ...
    (comp.lang.c)
  • Getting the "program name"
    ... In Standard of COBOL, ... POTENTIALLY distinct in some compilers and operating systems: ... In other environments (compiler / OS combinations) it might be ...
    (comp.lang.cobol)
  • Re: Is C99 the final C? (some suggestions)
    ... > that someone will try compile their stuff on an old compiler. ... > because the ANSI standard obsoleted them, and everyone picked up the ANSI ... fixed by using another language. ... >>are multiplying two expressions of the widest type supported by your ...
    (comp.lang.c)
  • Using restricted pointers with newly allocated arrays/structures
    ... return (int) x; ... The use of "restrict" here is intended to inform the compiler that x ... However, according to 6.7.3.1p4 of the standard, the code seems to ... based on restricted pointer x, is used to access the object x(and ...
    (comp.std.c)