Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   programming help (http://www.go4expert.com/forums/programming-help-t1269/)

altafahmed2k4 26Aug2006 18:11

programming help
 
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

shabbir 26Aug2006 19:32

Re: programming help
 
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 1Sep2006 12:21

Re: programming help
 
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.

shabbir 1Sep2006 14:08

Re: programming help
 
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 1Sep2006 15:43

Re: programming help
 
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 1Sep2006 15:51

Re: programming help
 
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 1Sep2006 17:24

Re: programming help
 
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 1Sep2006 17:31

Re: programming help
 
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.


All times are GMT +5.5. The time now is 02:33.