Comma operator

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).

The use of the comma token as an operator is distinct from its use in function calls and definitions, variable declarations, enum declarations, and similar constructs, where it acts as a separator.

Syntax[edit]

The comma operator separates expressions (which have value) in a way analogous to how the semicolon terminates statements, and sequences of expressions are enclosed in parentheses analogously to how sequences of statements are enclosed in braces: (a, b, c) is a sequence of expressions, separated by commas, which evaluates to the last expression c while {a; b; c;} is a sequence of statements, and does not evaluate to any value. Note that a comma can only occur between two expressions – commas separate expressions – unlike the semicolon, which occurs at the end of a (non-block) statement – semicolons terminate statements.

The comma operator has the lowest precedence of any C operator, and acts as a sequence point. In a combination of commas and semicolons, semicolons have lower precedence than commas, as semicolons separate statements but commas occur within statements, which accords with their use as ordinary punctuation: a, b; c, d is grouped as (a, b); (c, d) because these are two separate statements.

Examples[edit]

In this example, the differing behavior between the second and third lines is due to the comma operator having lower precedence than assignment. Note that the last example differs as well since the return expression must be fully evaluated before the function can return.

// Examples:               Descriptions:                                                                 Values after line is evaluated:
int a=1, b=2, c=3, i=0; // commas act as separators in this line, not as an operator 
                        // ... a=1, b=2, c=3, i=0
i = (a, b);             // stores b into i 
                        // ... a=1, b=2, c=3, i=2
i = a, b;               // stores a into i. Equivalent to (i = a), b;
                        // ... a=1, b=2, c=3, i=1
i = (a += 2, a + b);    // increases a by 2, then stores a+b = 3+2 into i
                        // ... a=3, b=2, c=3, i=5
i = a += 2, a + b;      // increases a by 2, then stores a to i, and discards unused
                        // a + b rvalue. Equivalent to (i = (a += 2)), a + b; 
                        // ... a=5, b=2, c=3, i=5
i = a, b, c;            // stores a into i, discarding the unused b and c rvalues
                        // ... a=5, b=2, c=3, i=5
i = (a, b, c);          // stores c into i, discarding the unused a and b rvalues
                        // ... a=5, b=2, c=3, i=3
return a=4, b=5, c=6;   // returns 6, not 4, since comma operator sequence points
                        // following the keyword 'return' are considered a single
                        // expression evaluating to rvalue of final subexpression c=6
return 1, 2, 3;         // returns 3, not 1, for same reason as previous example
return(1), 2, 3;        // returns 3, not 1, still for same reason as above.  This
                        // example works as it does because return is a keyword, not 
                        // a function call. Even though most compilers will allow for
                        // the construct return(value), the parentheses are syntactic
                        // sugar that get stripped out without syntactic analysis

In Pascal, multidimensional arrays are indexed using commas, e.g. A[i, j]. In C, however, A[i, j] is equivalent to A[j], since the value of i is discarded. The correct way to index multidimensional arrays in C is with a construction like A[i][j].

Uses[edit]

There appear to be few practical uses of operator,().


Bjarne Stroustrup, The Design and Evolution of C++

The comma operator has relatively limited use cases. Because it discards its first operand, it is generally only useful where the first operand has desirable side effects. Further, because it is rarely used outside of specific idioms, and easily mistaken with other commas or the semicolon, it is potentially confusing and error-prone. Nevertheless, there are certain circumstances where it is commonly used, notably in for loops.

For loops[edit]

The most common use is to allow multiple assignment statements without using a block statement, primarily in the initialization and the increment expressions of a for loop. This is the only idiomatic use in elementary C programming. In the following example, the order of the loop's initializers is significant:

void rev(char *s, size_t len)
{
    char *first;
    for (first = s, s += len - 1; s >= first; --s)
        putchar(*s);
}

An alternative solution to this problem is parallel assignment, which allows multiple assignments to occur within a single statement, and also uses a comma, though with different syntax and semantics. This is used in Go in its analogous for loop.[1]

The comma can also be used synonymously with the semicolon, particularly when the statements in question function similarly to a loop increment:

++p, ++q;
++p; ++q;

Macros[edit]

The comma can be used in preprocessor macros to perform multiple operations in the space of a single syntactic expression.

One common use is to provide custom error messages in failed assertions. This is done by passing a parenthesized expression list to the assert macro, where the first expression is an error string and the second expression is the condition being asserted. The assert macro outputs its argument verbatim on an assertion failure. The following is an example:

#include <stdio.h>
#include <assert.h>
 
int main ( void )
{
    int i;
    for (i=0; i<=9; i++)
    {
        assert( ( "i is too big!", i <= 4 ) );
        printf("i = %i\n", i);
    }
    return 0;
}

Output:

i = 0
i = 1
i = 2
i = 3
i = 4
assert: assert.c:6: test_assert: Assertion `( "i is too big!", i <= 4 )' failed.
Aborted

Condition[edit]

The comma can be used within a condition (of an if, while, do while, or for) to allow auxiliary computations, particularly calling a function and using the result, with block scoping:

if (y = f(x), y > x) {
    ... // statements involving x and y
}

A similar idiom exists in Go, where the syntax of the if statement explicitly allows an optional statement.[2]

Complex return[edit]

The comma can be used in return statements, to assign to a global variable or out parameter (passed by reference). This idiom suggests that the assignments are part of the return, rather than auxiliary assignments in a block that terminates with the actual return. For example, in setting a global error number:

if (failure)
    return (errno = EINVAL, -1);

This can be written more verbosely as:

if (failure) {
    errno = EINVAL;
    return -1;
}

Avoid a block[edit]

For brevity, the comma can be used to avoid a block and associated braces, as in:

if (x == 1) y = 2, z = 3;
if (x == 1)
    y = 2, z = 3;

instead of:

if (x == 1) {y = 2; z = 3;}
if (x == 1) {
    y = 2; z = 3;
}

Other languages[edit]

In the OCaml and Ruby programming languages, the semicolon (";") is used for this purpose. JavaScript and Perl utilize the comma operator in the same way C/C++ does.

See also[edit]

References[edit]

  1. ^ Effective Go: for, "Finally, Go has no comma operator and ++ and -- are statements not expressions. Thus if you want to run multiple variables in a for you should use parallel assignment (although that precludes ++ and --)."
  2. ^ The Go Programming Language Specification: If statements
  • Ramajaran, V. (1994), Computer Programming in C, New Delhi: Prentice Hall of India 
  • Dixit, J.B (2005), Fundamentals of computers and programming in C, New Delhi: Laxmi Publications 
  • Kernighan, Brian W.; Dennis M. Ritchie (1988), The C Programming Language (2nd ed.), Englewood Cliffs, NJ: Prentice Hall 

External links[edit]