C-Query
Sriharsha Vedurmudi
sriharsha.v@redpinesignals.com
Wed Sep 15 04:39:00 GMT 2004
Shashidhara S Bapat wrote:
> Hi All,
> I have doubts in the following C program: (I used gcc compiler)
>
> >>>
> #include <stdio.h>
>
> int main()
> {
> int i, j, b;
>
> i = 12, j = 1;
> b = ++i + ++i + i++;
Refrain from using such expressions as they are Undefined in ANSI. You
Cannot be sure of the answer (i.e. it can vary from compiler to compiler
and hence is not portable).
> printf("%d\n", b);
>
> i = 12, j = 1;
> b = j++ + ++j;
Read Above
> printf("%d\n", b);
>
> i = 12, j = 1;
> b = ++i + ++i + i++ + j++ + ++j ;
RE-READ above
> printf("%d\n", b);
>
> return 0;
> }
>
> and the output of the code is :
> 42
> 4
> 45.
>
> ..and my question is why do we get 42 in the first printf? (I was
> expecting 41 (13+14+14) O:-) ) and in the second printf why it
> isn't 3?
> (If it is because of the usage of stack (and/or tree) during the
> evaluation of those expressions, why dont we get 46 in the 3rd printf
> (42 + 4) ?).
>
> Does it depend on the compiler?
> Please let me know abt the reason.
UNDEFINED BEHAVIOUR
> thanks in advance.
>
More information about the Gcc-help
mailing list