programming help

altafahmed2k4's Avatar, Join Date: Aug 2006
Go4Expert Member
Code: CPP
main()
{
    int a=2;
    printf("%d %d %d",a,a++,++a);
}
for the above program igot the result as 4,3,3
but
Code: CPP
main()
{
    int a=2;
    printf("%d",a);
    printf("%d",a++);
    printf("%d",++a);
}
fetched me this result 2,2,4

may i know why?
altafahmed2k4@yahoo.co.in

Last edited by shabbir; 26Aug2006 at 19:30.. Reason: Code formating.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Welcome to G4EF.

Regarding the code snippets that you post in the threads please do use bb code to format the code snippets.

Because it calculates right to left.

See the out put of the following code
Code: CPP
main()
{
    int a=2;
    printf("%d",++a);
    printf("%d",a++);
    printf("%d",a);
}
Aztec's Avatar, Join Date: May 2006
Contributor
Quote:
Originally Posted by shabbir
Because it calculates right to left.
C standard doesn't say anything regarding order in which arguments are evaluated. It could be right-to-left , left-to-right or maybe something else. Try to write a code which is independent of order in which arguments are evaluated.
The OP's code has undefined behaviour.

Last edited by Aztec; 1Sep2006 at 12:26..
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Aztec
C standard doesn't say anything regarding order in which arguments are evaluated. It could be right-to-left , left-to-right or maybe something else. Try to write a code which is independent of order in which arguments are evaluated.
The OP's code has undefined behaviour.
Yes thats true and also its an unwritten fact that C compilers calculate right to left where as some programming language specially if I am correct its Fortran calculates it from left to right.
Aztec's Avatar, Join Date: May 2006
Contributor
Quote:
Originally Posted by shabbir
Yes thats true and also its an unwritten fact that C compilers calculate right to left
You can not say that. It might be happening this way in your compiler but there's no such rule.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Aztec
You can not say that. It might be happening this way in your compiler but there's no such rule.
I have tested the same on 5 main compilers when I was in my college

TurboC2, TurboC3, BorlandC, Microsoft Visual studio and gcc compiler but there is no rule for behaving such.
Aztec's Avatar, Join Date: May 2006
Contributor
TurboC2, TurboC3--->Total Crap
BorlandC, Microsoft Visual studio and gcc compiler--->Yaa right What should I assume about version numbers.
And why are you defending your wrong statement?
You can not assume any particular order. It varies from compiler to compiler.

In short, code has undefined behaviour.That's it.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Aztec
And why are you defending your wrong statement?
I am not defending the statement. I am just saying what I mentioned earlier its <<unwritten fact that C compilers calculate right to left>>. I knew what you are mentioning about the undefined behaviour and so I wrote it the unwritten fact. I never said it behaves in such a manner. isnt it.
Quote:
Originally Posted by Aztec
BorlandC
From 5.x,
Quote:
Originally Posted by Aztec
Microsoft Visual studio
6,7,8 and
Quote:
Originally Posted by Aztec
gcc
I dont remember which one it was.