473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

the order of printf parameter

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

Jan 11 '06 #1
6 2978
"marsarden" <ar*******@gmail.com> 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) 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.
Jan 11 '06 #2
Keith Thompson wrote:
"marsarden" <ar*******@gmail.com> 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.
Jan 11 '06 #3
marsarden wrote:
--------pq.c--------
#include <stdio.h>
void main()

^^^^
Dead already.
Jan 11 '06 #4
Keith Thompson wrote:
"marsarden" <ar*******@gmail.com> 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.

Jan 12 '06 #5
"Old Wolf" <ol*****@inspire.net.nz> writes:
Keith Thompson wrote:
"marsarden" <ar*******@gmail.com> 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) 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.
Jan 12 '06 #6
thanks alot

Jan 16 '06 #7

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

Similar topics

5
by: Peter Ammon | last post by:
It's my understanding that the printf() function is declared as int printf(const char * restrict format, ...); in stdio.h. And loosely speaking, if a parameter is declared as restricted, then...
3
by: Hans Ginzel | last post by:
Hello, let us consider function int foo(char *name, void *data) { ... printf(name, data); /* Should be *data? */ ... }
4
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues....
3
by: nandh_dp | last post by:
When the below program is compiled and executed, #include <stdio.h> #define MASK 0xFFFFFFULL main() { unsigned long long a; unsigned long b, c;
10
by: lovecreatesbeauty | last post by:
Is parameter type conversion required for the 2nd argument on printf("%p", (void *)&i); ? But one would never call memcpy like: memcpy((void *)pi, (void *)pj, sizeof *pj); /*memcpy((void *)pi,...
10
by: somenath | last post by:
Hi All, Please help me to understand the reason behind the output of the following program What is the output of the following program: int i=10; int f1() { static int i = 15; printf("f1:%d...
35
by: Francine.Neary | last post by:
I'm finding it really hard to keep some of these things straight... For example, fputs is to puts as fprintf is to printf, except that fputs has the file handle at the end and fprintf at the...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
2
yue219
by: yue219 | last post by:
Language is C++. I dont know how to take what the user orders and calculate their bill. I used a class. Heres my code: #include <iostream> #include <string> #include "Headerfile.h" using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.