Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:54253 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 31647 invoked from network); 29 Jul 2011 18:01:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Jul 2011 18:01:41 -0000 Authentication-Results: pb1.pair.com smtp.mail=joey@joeysmith.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=joey@joeysmith.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain joeysmith.com designates 209.90.98.146 as permitted sender) X-PHP-List-Original-Sender: joey@joeysmith.com X-Host-Fingerprint: 209.90.98.146 host-3.pl1071314.fiber.net Received: from [209.90.98.146] ([209.90.98.146:35065] helo=localhost) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 78/A2-01298-485F23E4 for ; Fri, 29 Jul 2011 14:01:40 -0400 Received: from joey by localhost with local (Exim 4.74) (envelope-from ) id 1QmrNM-0007N2-RX; Fri, 29 Jul 2011 12:01:32 -0600 Date: Fri, 29 Jul 2011 12:01:32 -0600 To: Adir Kuhn Cc: internals@lists.php.net Message-ID: <20110729180132.GA28037@joeysmith.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [PHP-DEV] c and php operations From: joey@joeysmith.com (Joey Smith) 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 > 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 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.