printf execution problem

R

rahul8143

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?
but can anybody have any other answer to this behaviour in terms of OS
or Compiler???
Thanks for help.
 
Z

Zara

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?
but can anybody have any other answer to this behaviour in terms of OS
or Compiler???
Thanks for help.

It depends on the compiler and on the level of optimization selected
for that complier.

Parameters of a function are evaluated in whichever order suits the
compiler. Std C90, 6.5, paragraph 1 renders undefined the sample you
have given us.
 
D

David Resnick

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?
but can anybody have any other answer to this behaviour in terms of OS
or Compiler???
Thanks for help.

Learn to read the group and FAQ before posting, this is asked all the
time.
http://www.eskimo.com/~scs/C-faq/q3.2.html

I wish google groups supported killfiles.

-David
 
R

rahul8143

David said:
Learn to read the group and FAQ before posting, this is asked all the
time.
http://www.eskimo.com/~scs/C-faq/q3.2.html
I have first read above link and then decided to ask question. The
link said that such programs behaviour is undefined and depends on
compiler. But i thought that is there any hidden relationship of this
behaviour to OS. Nowadays no one uses DOS but everybody says to use GCC
but most people also uses VC++. so doesn't it create a war of
compilers(which to use?)
 
L

Lew Pitcher

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

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-----
 
F

Flash Gordon

I have first read above link and then decided to ask question. The
link said that such programs behaviour is undefined and depends on
compiler. But i thought that is there any hidden relationship of this
behaviour to OS.

There may be, there may not. It really does not matter. All that matters
is that you never do anything like the above.
> Nowadays no one uses DOS but everybody says to use GCC
but most people also uses VC++. so doesn't it create a war of
compilers(which to use?)

Whichever you want, from the point of view of this group it makes little
difference since both can do a reasonable job of compiling code that
conforms to C89 and producing diagnostics where required. Each has
advantages and disadvantages. The best place to discuss the relative
merits of compilers for Windows, DOS or any other system is in a group
dedicated to that system.
 
J

Jaspreet

I have first read above link and then decided to ask question. The
link said that such programs behaviour is undefined and depends on
compiler. But i thought that is there any hidden relationship of this
behaviour to OS. Nowadays no one uses DOS but everybody says to use GCC
but most people also uses VC++. so doesn't it create a war of
compilers(which to use?)

If you have read the FAQ link then you should not be asking such a
question again and again. Does the link not say undefined behavior ? If
it is undefined then there is no point for me, you or any other Tom,
Dick or Harry to try and explain the output.

I would rather suggest you to study on sequence points. After that,
there are a whole lot of other topics which do give you some defined
behavior irrespective of what compilers or platform you are using. It
would prove more beneficial for all if you rather concentrate on those
topics and ask queries on those topics instead.

I wish David's suggestion of killfiles is taken note of.
 
K

Keith Thompson

I have first read above link and then decided to ask question. The
link said that such programs behaviour is undefined and depends on
compiler. But i thought that is there any hidden relationship of this
behaviour to OS. Nowadays no one uses DOS but everybody says to use GCC
but most people also uses VC++. so doesn't it create a war of
compilers(which to use?)

If you feel the need to ask a frequently asked question, it's a good
idea to mention that you've already read the FAQ *and* to make it
clear to us just what information you're looking for that the FAQ
doesn't already cover. Otherwise we'll assume you just haven't
bothered.

Apart from the obvious problem, your program also invokes undefined
behavior because it calls printf() with no prototype visible. You
*must* have a "#define <stdio.h>" for any program that uses printf().
(Or you could manually declare it yourself, but there's absolutely no
reason to do that.) Also, it's implementation-defined whether a
newline is required at the end of a text stream (such as stdout).

The statement
printf("%d %d %d\n", i++, i++, i++);
invokes undefined behavior because it modifies "i" multiple times
between sequence points. Undefined behavior means that the standard
imposes no requirements on what the program does; it can legally print
"Hello, Mr. Wilson" and then hide your socks in the refrigerator.

Why is the behavior undefined? Because the standard says so. Why
does the standard say so? For a number of reasons having to do with
making the compiler's job easier and allowing for optimizations, and
because there's not much point in defining the behavior of bad code.

The observed variations in the actual behavior *probably* have to do
with the compiler rather than the operating system, but the behavior
can vary from one run to the next, depending on compiler options or
the phase of the moon.

If you simply avoid writing such code (and there's no real reason to
do so in the first place), you don't have to worry about any of this.
 
R

rahul8143

Keith said:
If you feel the need to ask a frequently asked question, it's a good
idea to mention that you've already read the FAQ *and* to make it
clear to us just what information you're looking for that the FAQ
doesn't already cover. Otherwise we'll assume you just haven't
bothered.

Apart from the obvious problem, your program also invokes undefined
behavior because it calls printf() with no prototype visible. You
*must* have a "#define <stdio.h>" for any program that uses printf().
(Or you could manually declare it yourself, but there's absolutely no
reason to do that.) Also, it's implementation-defined whether a
newline is required at the end of a text stream (such as stdout).

The statement
printf("%d %d %d\n", i++, i++, i++);
invokes undefined behavior because it modifies "i" multiple times
between sequence points. Undefined behavior means that the standard
imposes no requirements on what the program does; it can legally print
"Hello, Mr. Wilson" and then hide your socks in the refrigerator.

Why is the behavior undefined? Because the standard says so. Why
does the standard say so? For a number of reasons having to do with
making the compiler's job easier and allowing for optimizations, and
because there's not much point in defining the behavior of bad code.

The observed variations in the actual behavior *probably* have to do
with the compiler rather than the operating system, but the behavior
can vary from one run to the next, depending on compiler options or
the phase of the moon.

If you simply avoid writing such code (and there's no real reason to
do so in the first place), you don't have to worry about any of this.
That is a nice explanation to my question Keith. Also Thanks to Flash
and Lew for taking your valuable time to answer my so called useless
question as its given in FAQ.
I am sorry that i should have mentioned in first post that i have read
FAQ. while posting again i will take care of this thing.
 
E

Emmanuel Delahaye

Does printf output depends on compilers or operating systems? I have
one program

No, but using it badly invokes an undefined behaviour.
int main()
{
int i=5;
printf("%d %d %d",i++,i++,i++);

The order of evaluation of function parameters is not defined by the
standard. Make no assumptions...

Additionally, I consider beeing bad style the use of a unary operator
in a function parameter. I don't do that.

Experts would probably talk about "missing sequence points".
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?
Yes.

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

Not here (in c.l.c).
Thanks for help.

BTW, variadic functions require a prototype. <stdio.h> is missing.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top