programming help

Go4Expert Member
26Aug2006,18:11   #1
altafahmed2k4's Avatar
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.
Go4Expert Founder
26Aug2006,19:32   #2
shabbir's Avatar
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);
}
Contributor
1Sep2006,12:21   #3
Aztec's Avatar
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..
Go4Expert Founder
1Sep2006,14:08   #4
shabbir's Avatar
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.
Contributor
1Sep2006,15:43   #5
Aztec's Avatar
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.
Go4Expert Founder
1Sep2006,15:51   #6
shabbir's Avatar
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.
Contributor
1Sep2006,17:24   #7
Aztec's Avatar
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.
Go4Expert Founder
1Sep2006,17:31   #8
shabbir's Avatar
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.