I think you need to look up what it means for behaviour to be undefined.

I valid, working C compiler can print *anything* for the result of
k=++j + ++j + ++j;  Anything.  You cannot rely on observed behaviour.
The answer might change if you switch certain compiler optimisations
on, change compiler version, change compiler entirely.

The specification says that a C compiler can do anything here and
still be correct.  If you were writing unit tests for a compiler from
the specification, you would not write a unit test for this scenario,
because the behaviour is undefined - the specification does not define
an expected result.

Paul Smith

[email protected]



On Mon, Aug 15, 2011 at 7:37 PM, sumon <[email protected]> wrote:
>
>
> On Aug 15, 10:25 pm, Paul Smith <[email protected]> wrote:
>> The behaviour of your code is undefined.
>>
>> That means you should never write code in this manner - there's no
>> need of multiple increment operations within the same expression.
>>
>> Paul Smith
>>
>> [email protected]
>>
>>
>>
>>
>>
>>
>>
>> On Sun, Aug 14, 2011 at 10:54 AM, sumon <[email protected]> wrote:
>> > #include<stdio.h>
>> > void main()
>> > {
>> >        int j=2,k;
>> >        k=++j + ++j + ++j;
>> >        printf("%d",k);
>>
>> > }
>>
>> > In GCC compiler why is it  giving 13??
>>
>> > --
>> > You received this message because you are subscribed to the Google Groups 
>> > "google-codejam" group.
>> > To post to this group, send email to [email protected].
>> > To unsubscribe from this group, send email to 
>> > [email protected].
>> > For more options, visit this group 
>> > athttp://groups.google.com/group/google-code?hl=en.
>
> yes.. i know, but i am checking the sequence of execution in a
> expression!
> k=++j + (++j + ++j); it gives 15!
>
> --
> You received this message because you are subscribed to the Google Groups 
> "google-codejam" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/google-code?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en.

Reply via email to