Comma operator: Difference between revisions
m →Syntax: link |
This is just incorrect, does not compile, and is not valid c++. Tag: section blanking |
||
Line 49: | Line 49: | ||
{{expand-section|date=December 2012}} |
{{expand-section|date=December 2012}} |
||
The comma can be used in preprocessor macros to perform multiple operations in the space of a single syntactic expression. |
The comma can be used in preprocessor macros to perform multiple operations in the space of a single syntactic expression. |
||
===Condition=== |
|||
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]]: |
|||
<source lang="C"> |
|||
if (int y = f(x), y > x) { |
|||
... // statements involving x and y |
|||
} |
|||
</source> |
|||
===Complex return=== |
===Complex return=== |
Revision as of 04:02, 11 July 2013
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
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 parenthesis 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
In this example, the differing behavior between the second and third lines is due to the comma operator having lower precedence than assignment.
// Examples: Descriptions: Values after line is evaluated:
int a=1, b=2, c=3, i=0; // comma acts as separator 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 into i. Equivalent to (i = (a += 2)), a + b; ... a=5, b=2, c=3, i=5
i = a, b, c; // stores a into i ... a=5, b=2, c=3, i=5
i = (a, b, c); // stores c into i ... a=5, b=2, c=3, i=3
Note that comma cannot be used in indexing multidimensional array: the code A[i, j]
evaluates to A[j]
with the i
discarded, instead of the correct A[i][j].
This differs from the syntax in Pascal, where A[i, j]
is correct, and can be a source of errors.
Uses
There appear to be few practical uses of
operator,().
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
The most common use is to allow compound assignment 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);
}
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
![]() | This section needs expansion. You can help by adding to it. (December 2012) |
The comma can be used in preprocessor macros to perform multiple operations in the space of a single syntactic expression.
Complex return
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
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
In the OCaml and Ruby programming languages, the semicolon (";") is used for this purpose. JavaScript utilizes the comma operator in the same way C/C++ does.
See also
References
- 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. (1988), The C Programming Language (2nd ed.), Englewood Cliffs, NJ: Prentice Hall
{{citation}}
: Unknown parameter|coauthors=
ignored (|author=
suggested) (help)
External links
- "Effect of using a comma instead of a semi-colon in C and C++", Stack Overflow
- Boost.Assignment – facility from the Boost library that makes it easy to fill containers by overloading the comma operator