x = y++ + x++ operation wrongly implemented on solaris version. gcc Version: chinar: /user/ggoel/Release/steel > gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/specs gcc version 2.8.1 chinar: /user/ggoel/Release/steel > Program code: #include<stdio.h> main() { int x=20,y=35; x = y++ + x++; printf("%d\n",x,y); } Output of program: 21 Expected output 56: Comments: this problem is there with other compilers: cc and g++. Same problen is not apearing on Linux. OS version: chinar: /user/ggoel/Release/steel > uname -rsv SunOS 5.6 Generic_105181-35 chinar: /user/ggoel/Release/steel > Other details: chinar: /user/ggoel/Release/steel > gcc -v temp.c Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/specs gcc version 2.8.1 /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/cpp -lang-c -v -undef - D__GNUC__=2 -D__GNUC_MINOR__=8 -Dsparc -Dsun -Dunix -D__svr4__ -D__SVR4 - D__sparc__ -D__sun__ -D__unix__ -D__svr4__ -D__SVR4 -D__sparc -D__sun -D__unix - Asystem(unix) -Asystem(svr4) -D__GCC_NEW_VARARGS__ -Acpu(sparc) -Amachine (sparc) temp.c /var/tmp/ccava4ZP.i GNU CPP version 2.8.1 (sparc) #include "..." search starts here: #include <...> search starts here: /usr/local/include /usr/local/sparc-sun-solaris2.6/include /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/include /usr/include End of search list. /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/cc1 /var/tmp/ccava4ZP.i - quiet -dumpbase temp.c -version -o /var/tmp/ccava4ZP.s GNU C version 2.8.1 (sparc-sun-solaris2.6) compiled by GNU C version 2.8.1. /usr/ccs/bin/as -V -Qy -s -o /var/tmp/ccava4ZP1.o /var/tmp/ccava4ZP.s /usr/ccs/bin/as: WorkShop Compilers 4.X dev 18 Sep 1996 /usr/ccs/bin/ld -V -Y P,/usr/ccs/lib:/usr/lib -Qy /usr/local/lib/gcc-lib/sparc- sun-solaris2.6/2.8.1/crt1.o /usr/local/lib/gcc-lib/sparc-sun- solaris2.6/2.8.1/crti.o /usr/ccs/lib/values-Xa.o /usr/local/lib/gcc-lib/sparc- sun-solaris2.6/2.8.1/crtbegin.o -L/usr/local/lib/gcc-lib/sparc-sun- solaris2.6/2.8.1 -L/usr/local/sparc-sun-solaris2.6/lib -L/usr/ccs/bin - L/usr/ccs/lib -L/usr/local/lib /var/tmp/ccava4ZP1.o -lgcc -lc - lgcc /usr/local/lib/gcc-lib/sparc-sun- solaris2.6/2.8.1/crtend.o /usr/local/lib/gcc-lib/sparc-sun- solaris2.6/2.8.1/crtn.o ld: Software Generation Utilities - Solaris Link Editors: 5.6-1.274 chinar: /user/ggoel/Release/steel >
*** This bug has been marked as a duplicate of 11751 ***
Hi, It seems to me that this problem is different for what defined in other bug. Here we are assigning a value after adding and the variable with other constant, but instead of adding then incrementing the variable compiler is just incrementing the value. Behavior is different in Linux. Code is: x=20; y=35; x = y++ + x++; For this expression compiler should first add x and y and then increment x. This expression should be broken like this. x = x + y; y++; x++; but compiler on Solaris is doing something like this: temp = x++; temp1 = y++; x = x + y; y = temp1; x = temp; /* reassigning the variable with incremented value. This is wrong */ expected value of x here is 56, but its giving 21. Same is working fine with Linux. this problem is there with Solaris and compilers cc and g++ also. Regards, Gaurav
Nope x = x++; is undefined so is x = 1 + x++; or x = y++ + x++;, you are modifing x twice in without a sequence point inbetween. *** This bug has been marked as a duplicate of 11751 ***