This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Can someone explain why this code outputs 7 ? ïIs this a gcc bug?)
- From: Prathamesh Kulkarni <prathamesh dot kulkarni at linaro dot org>
- To: chen at center dot wakayama-u dot ac dot jp
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Sat, 23 Apr 2016 17:38:48 +0530
- Subject: Re: Can someone explain why this code outputs 7 ? ïIs this a gcc bug?)
- Authentication-results: sourceware.org; auth=none
- References: <20160423113629 dot 00004A89 dot 0870 at center dot wakayama-u dot ac dot jp>
On 23 April 2016 at 17:06, <chen@center.wakayama-u.ac.jp> wrote:
> The following code gives 7 when compiled with gcc.
>
> I think, the value of the three phase "++i" should be 1, 2, 3, although
> the order may be different. So, the sum of them should be 6.
>
> I have confirmed that it gives 6 when compiled with clang.
>
> I know that a program having common sense will not write code like this,
> but I still can not understand this behavior of gcc.
>
> /******************************************/
> #include <stdio.h>
> int main()
> {
> int i = 0;
> printf("%d\n", (++i) + (++i) + (++i));
> return 0;
> }
Please see:
[1] https://gcc.gnu.org/bugs/#nonbugs
[2] http://c-faq.com/expr/seqpoints.html
Thanks,
Prathamesh
>
>