Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > the order of printf parameter

Reply
Thread Tools

the order of printf parameter

 
 
marsarden
Guest
Posts: n/a
 
      01-11-2006
--------pq.c--------
#include <stdio.h>
void main()
{
int a=2,*p,*q;
p=&a;q=&a;
printf("%d %d\n",*p++,*(q++));
printf("%d\n",a);
p=&a;q=&a;
printf("%d %d\n",*p,(*q)++);
}
-----------------------------------------
in gcc it output:

2 2
2
3 2

in vc6 it output:
2 2
2
2 2

anyone can tell me how the printf works?

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-11-2006
"marsarden" <> writes:
> --------pq.c--------
> #include <stdio.h>
> void main()


Find yourself a chalkboard and a piece of chalk (a whiteboard and a
marker will do if necessary). Write 100 times:
"main() returns int."

Change the line to "int main(void)".

> {
> int a=2,*p,*q;
> p=&a;q=&a;


Ok, p and q both point to a, which has the value 2.

> printf("%d %d\n",*p++,*(q++));


This should always print "2 2". You increment both pointers after
dereferencing them, which is fairly pointless, but it's harmless.

> printf("%d\n",a);


This will print "2".

> p=&a;q=&a;


Now p and q both point to a again.

> printf("%d %d\n",*p,(*q)++);


Here the third argument increments the object that q points to (namely a).

But as for any function call, the order of evaluation of the arguments
is unspecified.

Finally, since main() returns int, you should have a "return 0;" here.

> }
> -----------------------------------------
> in gcc it output:
>
> 2 2
> 2
> 3 2
>
> in vc6 it output:
> 2 2
> 2
> 2 2


Since the order of evaluation is unspecified, both results are valid.

If you care about the output, write the call so that it will work
properly and consistently regardless of the order of evaluation of the
arguments. For example, do the increment before or after the call,
not in the middle of it. If you keep your code simple and readable,
you don't have to *care* about details like this.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      01-11-2006
Keith Thompson wrote:
> "marsarden" <> writes:


<snip>

>> p=&a;q=&a;

>
> Now p and q both point to a again.
>
>> printf("%d %d\n",*p,(*q)++);

>
> Here the third argument increments the object that q points to (namely a).
>
> But as for any function call, the order of evaluation of the arguments
> is unspecified.


<snip>

>> in gcc it output:
>>
>> 2 2
>> 2
>> 3 2
>>
>> in vc6 it output:
>> 2 2
>> 2
>> 2 2

>
> Since the order of evaluation is unspecified, both results are valid.


It's worse than that surely? The value of a is being modified by (*q)++
and read for a purpose other than calculating the new value by *p with
no intervening sequence point, so surely this is undefined behaviour?

To the OP, undefined behaviour means that anything at all is valid, even
the program causing the HD in the computer to spin up to 10000000rpm and
then fly apart killing everyone in a 100 foot radius. Although in this
case the results you are seeing are more likely.

> If you care about the output, write the call so that it will work
> properly and consistently regardless of the order of evaluation of the
> arguments. For example, do the increment before or after the call,
> not in the middle of it. If you keep your code simple and readable,
> you don't have to *care* about details like this.


Agreed.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-11-2006
marsarden wrote:
> --------pq.c--------
> #include <stdio.h>
> void main()

^^^^
Dead already.
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      01-12-2006
Keith Thompson wrote:
> "marsarden" <> writes:
>>
>> int a=2,*p,*q;
>> p=&a;q=&a;

>
> Ok, p and q both point to a, which has the value 2.
>
>> printf("%d %d\n",*p,(*q)++);

>
> Here the third argument increments the object that q points to (namely a).
>
> But as for any function call, the order of evaluation of the arguments
> is unspecified.


Unfortunately, *p and *q are the same object. So, as
Flash (saviour of the universe) suggested, we definitiely
have UB: there is no sequence point between the write of *q,
and the read of *p.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-12-2006
"Old Wolf" <> writes:
> Keith Thompson wrote:
>> "marsarden" <> writes:
>>>
>>> int a=2,*p,*q;
>>> p=&a;q=&a;

>>
>> Ok, p and q both point to a, which has the value 2.
>>
>>> printf("%d %d\n",*p,(*q)++);

>>
>> Here the third argument increments the object that q points to (namely a).
>>
>> But as for any function call, the order of evaluation of the arguments
>> is unspecified.

>
> Unfortunately, *p and *q are the same object. So, as
> Flash (saviour of the universe) suggested, we definitiely
> have UB: there is no sequence point between the write of *q,
> and the read of *p.


You're right, I missed that.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
marsarden
Guest
Posts: n/a
 
      01-16-2006
thanks alot

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
order of printf happy C Programming 16 01-13-2010 10:02 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57