This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: gcc gives inccorect value for simple expression



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;
}





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]