Connecting Tech Pros Worldwide Forums | Help | Site Map

question about printf

pratik
Guest
 
Posts: n/a
#1: Nov 14 '05
i am working a turbo c;
the code i typed in is as follows
#include<stdio.h>
main()
{
int a=5;
printf("%d%d%d",a++,++a,a);
}
The output of the above code is very interesting.
can anyone help me with the output of the above code
Please tell me the reason for the same
Thanking you
in advance

Thomas stegen
Guest
 
Posts: n/a
#2: Nov 14 '05

re: question about printf


pratik wrote:[color=blue]
> i am working a turbo c;
> the code i typed in is as follows
> #include<stdio.h>
> main()
> {
> int a=5;
> printf("%d%d%d",a++,++a,a);
> }
> The output of the above code is very interesting.
> can anyone help me with the output of the above code[/color]

1234 hello 42666

is one possible output

Read the FAQ! It is all there.

--
Thomas.

Case
Guest
 
Posts: n/a
#3: Nov 14 '05

re: question about printf


pratik wrote:[color=blue]
> i am working a turbo c;
> the code i typed in is as follows
> #include<stdio.h>
> main()
> {
> int a=5;
> printf("%d%d%d",a++,++a,a);
> }
> The output of the above code is very interesting.
> can anyone help me with the output of the above code
> Please tell me the reason for the same[/color]

Although I wonder why you ask a question about something
you do not supply: the output, I can tell you the reason
of your surprize.

In C the order in which function arguments are evaluated
is not specified. So you might expect to see 577 but it
might as well be 665. Here's a question for you: How many
possible results are there (on different implementations
I mean)?

Case

Vijay Kumar R Zanvar
Guest
 
Posts: n/a
#4: Nov 14 '05

re: question about printf



"pratik" <pratikthakkar007@yahoo.com> wrote in message news:c14f7963.0405260222.72d65f59@posting.google.c om...[color=blue]
> i am working a turbo c;
> the code i typed in is as follows
> #include<stdio.h>
> main()
> {
> int a=5;
> printf("%d%d%d",a++,++a,a);
> }
> The output of the above code is very interesting.
> can anyone help me with the output of the above code
> Please tell me the reason for the same
> Thanking you
> in advance[/color]

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.

F:\Vijay\C> gcc printf_params.c -Wall
printf_params.c: In function `main':
printf_params.c:5: warning: operation on `a' may be undefined
printf_params.c:5: warning: operation on `a' may be undefined
printf_params.c:5: warning: operation on `a' may be undefined

Consider an example,

(*fptr)( (i=2), k );

Here, which expression -- i.e., "fptr", (i=2), or k -- would be evaluated first
is unspecified by the standard. And in the same way, which argument will pushed
onto the stack -- if that is _the_ method of parameter passing -- is also
unspecified. However, it is guaranteed that all the side effects would take
place before the function is called.

Your answer is particular to the compiler you are using, though, generally
passing parameters from right to left seems to be common.

There are many ways in which parameters can be passed to a function. This
again depends on the underlying CPU architecture. Many heavy-performance based
CPUs (sorry, don't know any particulare name) may have separate sets of
registers for parameter passing; the compiler doesn't even use stack! For
maximum portability of programs, no assumptions should be made. Consider
another example:

void
mango ( register int a, int b )
{
/* .. */
}

int
main ( void )
{
int k, j;
/* .. */
mango ( k, j );
return 0;
}

Assume that the compiler fulfills the request to use a register for the
first parameter (what if only one register is available?). How about the
second parameter?

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/


Daniel Fischer
Guest
 
Posts: n/a
#5: Nov 14 '05

re: question about printf


pratik @ 2004-05-26:[color=blue]
> printf("%d%d%d",a++,++a,a);[/color]

Order of execution is only defined for sequences, not between sequence points.
There aren't any sequence points in an argument list, and that's why the
behaviour of your code is undefined.

--
Danny
Richard Bos
Guest
 
Posts: n/a
#6: Nov 14 '05

re: question about printf


"Vijay Kumar R Zanvar" <vijoeyz@globaledgesoft.com> wrote:
[color=blue]
> "pratik" <pratikthakkar007@yahoo.com> wrote in message news:c14f7963.0405260222.72d65f59@posting.google.c om...[color=green]
> > #include<stdio.h>
> > main()
> > {
> > int a=5;
> > printf("%d%d%d",a++,++a,a);
> > }
> > The output of the above code is very interesting.[/color]
>
> 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.[/color]

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.
[color=blue]
> F:\Vijay\C> gcc printf_params.c -Wall
> printf_params.c: In function `main':
> printf_params.c:5: warning: operation on `a' may be undefined
> printf_params.c:5: warning: operation on `a' may be undefined
> printf_params.c:5: warning: operation on `a' may be undefined[/color]

Note that gcc gets it almost right, and more right than you did: those
operations on a may be, indeed they definitely _are_, undefined, which
is quite a bit worse than unspecified.

Richard
Case
Guest
 
Posts: n/a
#7: Nov 14 '05

re: question about printf


Richard Bos wrote:[color=blue]
> "Vijay Kumar R Zanvar" <vijoeyz@globaledgesoft.com> wrote:
>
>[color=green]
>>"pratik" <pratikthakkar007@yahoo.com> wrote in message news:c14f7963.0405260222.72d65f59@posting.google.c om...
>>[color=darkred]
>>>#include<stdio.h>
>>>main()
>>>{
>>> int a=5;
>>> printf("%d%d%d",a++,++a,a);
>>>}
>>>The output of the above code is very interesting.[/color]
>>
>> 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.[/color]
>
>
> 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.[/color]

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.

Kees

Richard Bos
Guest
 
Posts: n/a
#8: Nov 14 '05

re: question about printf


Case <no@no.no> wrote:
[color=blue]
> Richard Bos wrote:[color=green]
> > "Vijay Kumar R Zanvar" <vijoeyz@globaledgesoft.com> wrote:
> >[color=darkred]
> >>"pratik" <pratikthakkar007@yahoo.com> wrote in message
> >>
> >>> printf("%d%d%d",a++,++a,a);[/color][/color][/color]
[color=blue][color=green][color=darkred]
> >> 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.[/color]
> >
> > 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.[/color]
>
> 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.[/color]

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
Dan Pop
Guest
 
Posts: n/a
#9: Nov 14 '05

re: question about printf


In <c14f7963.0405260222.72d65f59@posting.google.com > pratikthakkar007@yahoo.com (pratik) writes:
[color=blue]
>i am working a turbo c;
>the code i typed in is as follows
>#include<stdio.h>
>main()
>{
> int a=5;
> printf("%d%d%d",a++,++a,a);
>}
>The output of the above code is very interesting.[/color]

Nope, you, your program and its output (if any) are very boring.

Don't post again before reading the FAQ.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
Martin Ambuhl
Guest
 
Posts: n/a
#10: Nov 14 '05

re: question about printf


pratik wrote:
[color=blue]
> i am working a turbo c;
> the code i typed in is as follows
> #include<stdio.h>
> main()
> {
> int a=5;
> printf("%d%d%d",a++,++a,a);
> }
> The output of the above code is very interesting.
> can anyone help me with the output of the above code
> Please tell me the reason for the same[/color]

Please check the archives on groups.google.com and the FAQ. This is so
old and boring that it's hard to keep from nodding off just reading your
post. Look up 'sequence point' in your text.

And learn to declare main as returning an int. It does so, and any
compilers implementing the current standard will require you to say so.
It is also best to return values from functions declared as returning
them, even though the new standard has given in to the illiterates and
made main act as if the return statement is there whether it is or not.
Keith Thompson
Guest
 
Posts: n/a
#11: Nov 14 '05

re: question about printf


pratikthakkar007@yahoo.com (pratik) writes:[color=blue]
> i am working a turbo c;
> the code i typed in is as follows
> #include<stdio.h>
> main()
> {
> int a=5;
> printf("%d%d%d",a++,++a,a);
> }[/color]

The C FAQ is at <http://www.eskimo.com/~scs/C-faq/faq.html>.
Read section 3. Then read the whole thing.

--
Keith Thompson (The_Other_Keith) kst-u@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.
Jack Klein
Guest
 
Posts: n/a
#12: Nov 14 '05

re: question about printf


On Wed, 26 May 2004 12:34:06 +0200, Case <no@no.no> wrote in
comp.lang.c:
[color=blue]
> pratik wrote:[color=green]
> > i am working a turbo c;
> > the code i typed in is as follows
> > #include<stdio.h>
> > main()
> > {
> > int a=5;
> > printf("%d%d%d",a++,++a,a);
> > }
> > The output of the above code is very interesting.
> > can anyone help me with the output of the above code
> > Please tell me the reason for the same[/color]
>
> Although I wonder why you ask a question about something
> you do not supply: the output, I can tell you the reason
> of your surprize.
>
> In C the order in which function arguments are evaluated
> is not specified. So you might expect to see 577 but it
> might as well be 665. Here's a question for you: How many
> possible results are there (on different implementations
> I mean)?
>
> Case[/color]

Sorry, but you are dead wrong here.

It is true that the order of evaluation of arguments to functions is
unspecified, but that has nothing to do with the possible output of
this program.

The program invokes undefined behavior, because the object a is
modified two times, and read yet a third time for a purpose other than
computing the new value, all without a sequence point.

You might get 577, you might get 665, but since the behavior is
undefined neither the C standard nor the participants of this group
place any requirements (or limits) on what may happen.

Executing this program may launch nuclear missiles, or cause your
computer to play Haydn's Trumpet Concerto (lovely piece, that), even
if it lacks a sound card and speakers. Of it could merely do the
traditional and reformat your hard disk.

Specifically see http://www.eskimo.com/~scs/C-faq/q3.2.html in the
FAQ, and the rest of the FAQ is certainly worth a read as well.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mark McIntyre
Guest
 
Posts: n/a
#13: Nov 14 '05

re: question about printf


On 26 May 2004 03:22:34 -0700, in comp.lang.c , pratikthakkar007@yahoo.com
(pratik) wrote:
[color=blue]
> printf("%d%d%d",a++,++a,a);
>The output of the above code is very interesting.[/color]

This is a FAQ. Please read it.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Mark McIntyre
Guest
 
Posts: n/a
#14: Nov 14 '05

re: question about printf


On 26 May 2004 03:22:34 -0700, in comp.lang.c , pratikthakkar007@yahoo.com
(pratik) wrote:
[color=blue]
> printf("%d%d%d",a++,++a,a);
>The output of the above code is very interesting.[/color]

This is a FAQ. Please read it.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Mark McIntyre
Guest
 
Posts: n/a
#15: Nov 14 '05

re: question about printf


On 26 May 2004 03:22:34 -0700, in comp.lang.c , pratikthakkar007@yahoo.com
(pratik) wrote:
[color=blue]
> printf("%d%d%d",a++,++a,a);
>The output of the above code is very interesting.[/color]

This is a FAQ. Please read it.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Joona I Palaste
Guest
 
Posts: n/a
#16: Nov 14 '05

re: question about printf


Mark McIntyre <markmcintyre@spamcop.net> scribbled the following:[color=blue]
> On 26 May 2004 03:22:34 -0700, in comp.lang.c , pratikthakkar007@yahoo.com
> (pratik) wrote:[color=green]
>> printf("%d%d%d",a++,++a,a);
>>The output of the above code is very interesting.[/color][/color]
[color=blue]
> This is a FAQ. Please read it.[/color]

I think reading it once will do...

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Life without ostriches is like coffee with milk."
- Mika P. Nieminen
Mark McIntyre
Guest
 
Posts: n/a
#17: Nov 14 '05

re: question about printf


On 27 May 2004 15:16:40 GMT, in comp.lang.c , Joona I Palaste
<palaste@cc.helsinki.fi> wrote:
[color=blue]
>Mark McIntyre <markmcintyre@spamcop.net> scribbled the following:[color=green]
>> On 26 May 2004 03:22:34 -0700, in comp.lang.c , pratikthakkar007@yahoo.com
>> (pratik) wrote:[color=darkred]
>>> printf("%d%d%d",a++,++a,a);
>>>The output of the above code is very interesting.[/color][/color]
>[color=green]
>> This is a FAQ. Please read it.[/color]
>
>I think reading it once will do...[/color]

yeah, sorry about that. My news-server threw some sort of fit and told my
client that it had not recieved the postings, so my client kindly kept
posting til they "got through".....
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
CBFalconer
Guest
 
Posts: n/a
#18: Nov 14 '05

re: question about printf


Joona I Palaste wrote:[color=blue]
> Mark McIntyre <markmcintyre@spamcop.net> scribbled the following:[color=green]
>> (pratik) wrote:
>>[color=darkred]
>>> printf("%d%d%d",a++,++a,a);
>>> The output of the above code is very interesting.[/color][/color]
>[color=green]
>> This is a FAQ. Please read it.[/color]
>
> I think reading it once will do...[/color]

Either Mark was being emphatic, or his sending finger has palsy.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Closed Thread