Re: c and php operations

From: Date: Fri, 29 Jul 2011 18:01:32 +0000
Subject: Re: c and php operations
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-54253@lists.php.net to get a copy of this message
On Fri, Jul 29, 2011 at 02:20:17PM -0300, Adir Kuhn wrote: > Hello guys, > > while I'm studing some c codes i found this page: > http://www.nicksays.co.uk/2009/05/awesome-c-exam-question/ > > and this code: > > #include <stdio.h> > int func (int a, int b) { > static int c = 1; > return a + b * (c *= -1);} > int main () { > int a = 2, b = 3; > int c = func(a, b); > > a *= a++; > b *= ++b; I'm no expert in C, but my understanding is that the two expressions immediately above are UNDEFINED because there is only one sequence point, but the objects are modified twice (*= and ++). See http://c-faq.com/expr/seqpoints.html gcc appears to agree with me via the -Wsequence-point flag: joey@banshee:0$cat > a.c #include <stdio.h> int func (int a, int b) { static int c = 1; return a + b * (c *= -1); } int main () { int a = 2, b = 3; int c = func(a, b); a *= a++; b *= ++b; printf("%d %d %d %d\n", a, b, c, func(a, b)); } joey@banshee:0$gcc -Wsequence-point a.c a.c: In function ‘main’: a.c:12:4: warning: operation on ‘a’ may be undefined a.c:13:4: warning: operation on ‘b’ may be undefined If my interpretation is correct, I'm not sure how much time we need to spend around the question.

« previous php.internals (#54253) next »