This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: gcc gives inccorect value for simple expression
- From: Kyrill Tkachov <kyrylo dot tkachov at foss dot arm dot com>
- To: chen at center dot wakayama-u dot ac dot jp, gcc-bugs at gcc dot gnu dot org
- Date: Fri, 22 Apr 2016 14:21:42 +0100
- Subject: Re: gcc gives inccorect value for simple expression
- Authentication-results: sourceware.org; auth=none
- References: <20160422130107 dot 00007391 dot 0885 at center dot wakayama-u dot ac dot jp>
On 22/04/16 14:01, chen@center.wakayama-u.ac.jp wrote:
The following code, when compiled with gcc, it outputs "7".
I have tried it with gcc-4.8.3 and gcc-6.0.1-20160415, both on x86_64
linux, with options
-O0, -g, -O2. All of them gave the same answer.
When compiled with clang, it gives 6.
I know that it is an ill coded program, but I still can't understand why
it outputs 7 when compiled by gcc.
The code exhibits undefined behaviour and thus there are no guarantees
at all about the result.
http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points
As an aside, this list is for automated emails sent by the GCC bug tracker, so it's
unlikely your posts here will be spotted.
For future questions about using gcc please use the gcc-help mailing list.
Cheers,
Kyrill
/******************************************/
#include <stdio.h>
int main()
{
int i;
i = 0;
printf("%d\n", (++i) + (++i) + (++i));
return 0;
}