Connecting Tech Pros Worldwide Forums | Help | Site Map

printf() function

Newbie
 
Join Date: Apr 2007
Location: Kolkata, India
Posts: 4
#1: Apr 20 '07
Could anybody please give the output of the following line of C code and explain?

int x=10;
printf("%d\t%d\t%d\t%d\n",--x,x--,++x,x++);
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Apr 20 '07

re: printf() function


Quote:

Originally Posted by Animesh Mandal

Could anybody please give the output of the following line of C code and explain?

int x=10;
printf("%d\t%d\t%d\t%d\n",--x,x--,++x,x++);

Nobody in his right mind can give the output of that expression because it
causes undefined behaviour. Whenever you alter the value of a variable more
than once in a parameter list the results will be undefined because the order
of evaluation is unspecified.

kind regards,

Jos
Newbie
 
Join Date: Apr 2007
Posts: 18
#3: Apr 20 '07

re: printf() function


Quote:

Originally Posted by Animesh Mandal

Could anybody please give the output of the following line of C code and explain?

int x=10;
printf("%d\t%d\t%d\t%d\n",--x,x--,++x,x++);

As in C args are loaded into stack from right to left
so we start from x++(rightmost)

as it is post so x remains 10
then ++x execution point, till now previous post will increment by 1 then this pre will also increment 1 so x now 12

now x-- as this is post so x remain 12
then --x then previous post and this pre both decrement it by 1 each so x becomes 10 again

so answer should be like that

answer
10 12 12 10
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Apr 20 '07

re: printf() function


Quote:

Originally Posted by jesusdiehard

so answer should be like that

answer
10 12 12 10

Read my previous reply; this is all about undefined behaviour.

kind regards,

Jos
Newbie
 
Join Date: Apr 2007
Posts: 18
#5: Apr 21 '07

re: printf() function


Quote:

Originally Posted by JosAH

Read my previous reply; this is all about undefined behaviour.

kind regards,

Jos

sorry, i m not getting your undefined behavior theory.
To my best knowledge, arguments are loaded on stack in C from right to left and after loading each arguments, excecution sequence change the variable's value.

I have checked the code on VC++, and i m quite sure on GCC it is going to give the same result.
Newbie
 
Join Date: Apr 2007
Location: Melbourne, Australia
Posts: 19
#6: Apr 21 '07

re: printf() function


Quote:

Originally Posted by jesusdiehard

sorry, i m not getting your undefined behavior theory.
To my best knowledge, arguments are loaded on stack in C from right to left and after loading each arguments, excecution sequence change the variable's value.

I have checked the code on VC++, and i m quite sure on GCC it is going to give the same result.

This is incorrect - C does not necessarily 'read' from left-to-right when evaluating. It will obey typical order-of-operations rules, but if two operations have equal precedence, it may evaluate either one first depending on which it thinks is more efficient to do first at the time.

Thus any evaluation that depends solely on left-to-right calculation will be undefined.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Apr 21 '07

re: printf() function


Quote:

Originally Posted by jesusdiehard

sorry, i m not getting your undefined behavior theory.
To my best knowledge, arguments are loaded on stack in C from right to left and after loading each arguments, excecution sequence change the variable's value.

I have checked the code on VC++, and i m quite sure on GCC it is going to give the same result.

My terminology was incorrect; my apologies. The behaviour is "unspecified" as
stated by the C99 standard:
Quote:
[#10] The order of evaluation of the function designator,
the actual arguments, and subexpressions within the actual
arguments is unspecified, but there is a sequence point
before the actual call.
Unspecified behaviour is defined as follows:
Quote:
3.19
[#1] unspecified behavior
behavior where this International Standard provides two or
more possibilities and imposes no requirements on which is
chosen in any instance

[#2] EXAMPLE An example of unspecified behavior is the
order in which the arguments to a function are evaluated.
kind regards,

Jos
Reply