473,508 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf execution problem

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.

Nov 15 '05 #1
11 3457
ra*******@gmail.com 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?
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.

Nov 15 '05 #2

rahul8...@gmail.com 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?
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

Nov 15 '05 #3

David Resnick wrote:
rahul8...@gmail.com 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?
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 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?) I wish google groups supported killfiles.

-David


Nov 15 '05 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ra*******@gmail.com 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)

iD8DBQFDPU9TagVFX4UWr64RAjTrAKDs0QpHcH5dlrHsu43M0w xo+DSP1gCfXx9d
tTPMaN0noTDWtHDy7a+303M=
=oByo
-----END PGP SIGNATURE-----
Nov 15 '05 #5
ra*******@gmail.com wrote:
David Resnick wrote:
rahul8...@gmail.com 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?
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 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?)
I wish google groups supported killfiles.

-David


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.

Nov 15 '05 #6
ra*******@gmail.com wrote:
David Resnick wrote:
rahul8...@gmail.com 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;
}

<snip>
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.


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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #7
ra*******@gmail.com writes:
David Resnick wrote:
rahul8...@gmail.com 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?
> 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 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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8

Keith Thompson wrote:
ra*******@gmail.com writes:
David Resnick wrote:
rahul8...@gmail.com 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?
> 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 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.

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. --
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Nov 15 '05 #9
ra*******@gmail.com wrote on 30/09/05 :
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++
Nov 15 '05 #10
ra*******@gmail.com wrote on 30/09/05 :
<...> Nowadays no one uses DOS but everybody says to use GCC
but most people also uses VC++.


What are you talking about ? I also use Texas Instrument Code Composer
and Diab Compiler. Is it a felony ?

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

..sig under repair
Nov 15 '05 #11

Emmanuel Delahaye wrote:
ra*******@gmail.com wrote on 30/09/05 :
<...> Nowadays no one uses DOS but everybody says to use GCC
but most people also uses VC++.
What are you talking about ? I also use Texas Instrument Code Composer
and Diab Compiler. Is it a felony ?

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

.sig under repair


Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2687
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
9
1355
by: Eric Lilja | last post by:
Hello, I have two code snippets I want you to look at. My program compiles without warnings (warning level set to max, gcc 3.4.3) with either snippet but the latter one causes a segfault at...
188
17200
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
6
17663
by: David Mathog | last post by:
size_t varies from system to system. This occasionally leads to coding issues such as: (void) sprintf(msg,"length of buffer: %d",strlen(msg)); which worked fine on a 32 linux but triggered...
29
17495
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding...
14
6801
by: Harman Dhaliwal | last post by:
Hi, I am programming an in memory representation of a database and am at the tail end, hopefully, of implementation. But the vexing point is that my program runs fine with all my diagnostic...
36
35397
by: Debaser | last post by:
I've recently read in one of my old C books that puts() is a better function call with regard to performance than printf() in the following situation: puts("Some random text"); vs. ...
19
9834
by: RedDevilDan | last post by:
I am working on a Memory Footprint Reduction project. I came across an idea to disable all printf statements so that less memory is required. In addition, when there is no single printf statement,...
10
4542
by: Rahul | last post by:
Hi Everyone, I had a query reg printf(). I heard that it can't be used in ISR's as it is non-re-enterant. I didn't get as to why printf is non-re-enterant and why non-re-enterant library can't...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.