Re: Varied results for ++ operator in VC++/Unix.
adebaene_at_club-internet.fr
Date: 01/21/05
- Next message: William DePalo [MVP VC++]: "Re: execv hanged"
- Previous message: John Smith: "Re: Memory leak in public mac address code"
- In reply to: krishna: "Varied results for ++ operator in VC++/Unix."
- Next in thread: MSalters: "RE: Varied results for ++ operator in VC++/Unix."
- Messages sorted by: [ date ] [ thread ]
Date: 21 Jan 2005 04:42:32 -0800
krishna wrote:
> I executed a program containing the following piece of code and got
the
> results as below
> Code
> ------
> #include "stdio.h"
>
> int main()
> {
> int a = 1;
> printf("the val is %d\n", ++a + ++a );
> }
You are invoking undefined behaviour, because the expression "++a +
++a" does not contain any sequence point. More precisely : there is no
guarantee that the side-effect of the first "++a" (ie, incrementing a)
has been applied when the second "++a" is evaluated. With respect to
C++ standard, the behaviour is undefined, so both answers are equelly
correct.
This is stated in 5,4 in the C++ standard : "Between the previous and
next sequence point a scalar object shall have its stored value
modified at most once by the evaluation of an expression".
Arnaud
MVP - VC
- Next message: William DePalo [MVP VC++]: "Re: execv hanged"
- Previous message: John Smith: "Re: Memory leak in public mac address code"
- In reply to: krishna: "Varied results for ++ operator in VC++/Unix."
- Next in thread: MSalters: "RE: Varied results for ++ operator in VC++/Unix."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|