From owner-freebsd-questions Wed Mar 28 13:31:13 2001 Delivered-To: freebsd-questions@freebsd.org Received: from bcfw1d.bridge.com (bcfw1d.ext.bridge.com [167.76.159.31]) by hub.freebsd.org (Postfix) with ESMTP id 15FEE37B718 for ; Wed, 28 Mar 2001 13:31:09 -0800 (PST) (envelope-from tayers@bridge.com) Received: (from uucp@localhost) by bcfw1d.bridge.com (8.10.2/8.10.2) id f2SLW3o21761; Wed, 28 Mar 2001 15:32:04 -0600 (CST) Received: from (unknown [167.76.56.34]) by bcfw1d via smap (V2.1) id xma021703; Wed, 28 Mar 01 15:31:52 -0600 Received: from mnmailhost (mnmailhost.bridge.com [167.76.155.14]) by mail1srv.bridge.com (8.8.8/8.7.3) with SMTP id PAA08058; Wed, 28 Mar 2001 15:30:45 -0600 (CST) Received: from 89-7 by mnmailhost (SMI-8.6/SMI-4.1) id QAA15096; Wed, 28 Mar 2001 16:30:28 -0500 To: Subject: Re: multiple C assignments in one statement References: From: Tim Ayers Date: 28 Mar 2001 15:30:23 -0600 In-Reply-To: Linh Pham's message of "Wed, 28 Mar 2001 11:18:02 -0800 (PST)" Message-ID: <8zlpzimo.fsf@tim.bridge.com> Lines: 75 User-Agent: Gnus/5.0803 (Gnus v5.8.3) Emacs/20.5 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG >>>>> "L" == Linh Pham writes: L> On 2001-03-28, j mckitrick scribbled: L> # In the source code for the shortest DVD decoder to date, I saw statements L> # with multiple assignments (not declarations) all separated by commas. Does L> # this have any advantage, other than allowing a bunch of assignments to be L> # grouped in one statement without the need for braces? L> Statements are set apart by semi-colons in C/C++ rather than commas. L> The two blocks of code are the same: L> int a, b, c; L> a = 0; L> b = 3; L> c = a + b; L> [is the same as] L> int a, b, c; a = 0; b = 3; c = a + b; Yes, at an elementary level that is true. But this program works equally well int a, b, c; a = 0, b = 3, c = a + b; From an on-line C reference Comma Operator When two or more expressions are separated by the comma operator, they evaluate from left to right. The result has the type and value of the rightmost expression (although side effects of the other expressions, if any, do take place). The result is not an lvalue. In the following example, the value 1 is assigned to R , and the value 2 is assigned to T : R = T = 1, T += 2, T -= 1; Side effects for each expression are completed before the next expression is evaluated. L> For readability, it's best not to cram multiple statements onto one L> line. If you want really compact code, then you can not only cram L> multiple statements together (again, separated by semi-colons) but also L> use obfuscated methods of doing the same thing as other functions. L> Braces are normally used to separate code from another chunks of code, L> like breaking code blocks into functions, etc. Jonathan stated the code he was looking at was "the shortest DVD decoder to date." Typically "shortest" code is not written for readability. I have not seen this code, but it is most likely that the comma operator was used to omit braces to save characters. For example, if(a%2)b=7,c=4; is two characters shorter than if(a%2){b=7;c=4;} Or it was used to obfuscate the code a tiny bit. Obfuscation is often another fun feature of shortest code programs. Jonathan, I can't answer your question about general advantages of the comma operator. I would be surprised if it is more efficient or anything like that. I typically use it in tests (if, for, while statements) for the side-effects since I know all the actions occur, but only the last value is used for the test. Unfortunately I can't think of a good example off the top of my head. On the downside, the comma operator makes your code a little bit tougher for the next guy to understand. HTH and Hope you have a very nice day, :-) Tim Ayers (tayers@bridge.com) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message