Skip site navigation (1)Skip section navigation (2)
Date:      28 Mar 2001 15:30:23 -0600
From:      Tim Ayers <tayers@bridge.com>
To:        <freebsd-questions@FreeBSD.ORG>
Subject:   Re: multiple C assignments in one statement
Message-ID:  <8zlpzimo.fsf@tim.bridge.com>
In-Reply-To: Linh Pham's message of "Wed, 28 Mar 2001 11:18:02 -0800 (PST)"
References:  <Pine.BSF.4.33.0103281114000.35388-100000@q.closedsrc.org>

next in thread | previous in thread | raw e-mail | index | archive | help
>>>>> "L" == Linh Pham <lplist@closedsrc.org> 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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?8zlpzimo.fsf>