comma operator
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). There is a sequence point between these evaluations. The use of the comma token as an is distinct from its use in function calls and definitions, variable declarations, enum declarations, and similar constructs, where it acts as a .
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). There is a sequence point between these evaluations. The use of the comma token as an is distinct from its use in function calls and definitions, variable declarations, enum declarations, and similar constructs, where it acts as a . ==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 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. 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. As of C++20, the comma operator has been deprecated in subscripting expressions, to reduce confusion, and open up the future possibility of repurposing the syntax for multidimensional array indexing. In C++23, the ability to overload operator[] with multiple arguments was added making unparenthesised comma expressions unusable in subscripts. The comma operator is still usable and not deprecated in this context if the comma expression is surrounded by parentheses (as in a[(b,c)] ). ==Examples== ==Uses== 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 that must be sequenced before the second operand. 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 and in SFINAE. For embedded systems which may have limited debugging capabilities, the comma operator can be used in combination with a macro to seamlessly override a function call, to insert code just before the function call. ===For loops=== The comma operator allows multiple assignment statements without using a block statement, for example in the initialization and the increment expressions of a for loop. 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; s >= first; --s) { putchar(*s); } } An alternative solution to this problem in other languages 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. ===Macros=== 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: for (int i = 0; i ===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: 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. ===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); } ===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; ===Overloading=== C++ allows overloading the comma operator, as both a binary operator and as a non-member. Its signatures are: U& T::operator,(U& rhs); (inside class definition) U& operator,(const T& lhs, U& rhs); (outside class definition) This allows for DSL-style APIs, like the following: Logger logger; logger, "Hello, ", "world!"; ==Other languages== In the OCaml and Ruby programming languages, the semicolon (";") is used for this purpose. JavaScript, Perl and D utilize the comma operator in the same way C/C++ does. Some other languages with C-like syntax, such as Java and C#, do not have this feature, though C# supports a comma-separated list of expressions in the initializer and iterator of a statement, thus enabling an equivalent of this in this specific context. ==See also== ==References== ===Bibliography=== ==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
References
- Expressions - D Programming Language § Comma Expression · retrieved 2026-11-20 · rev 1353149340
Cite this reference
MLA "Expressions - D Programming Language § Comma Expression." 2026-11-20. https://dlang.org/spec/expression.html#comma_expression. Accessed 24 November 2026.
Chicago "Expressions - D Programming Language § Comma Expression." 2026-11-20. https://dlang.org/spec/expression.html#comma_expression.
APA encyclopegia.org. (2026). Expressions - D Programming Language § Comma Expression. Retrieved November 24, 2026, from https://dlang.org/spec/expression.html#comma_expression
- Is comma (,) operator or separator in Java? · retrieved 2026-11-20 · rev 1353149340
Cite this reference
MLA "Is comma (,) operator or separator in Java?." 2026-11-20. https://stackoverflow.com/questions/38733508/is-comma-operator-or-separator-in-java. Accessed 24 November 2026.
Chicago "Is comma (,) operator or separator in Java?." 2026-11-20. https://stackoverflow.com/questions/38733508/is-comma-operator-or-separator-in-java.
APA encyclopegia.org. (2026). Is comma (,) operator or separator in Java?. Retrieved November 24, 2026, from https://stackoverflow.com/questions/38733508/is-comma-operator-or-separator-in-java
- Statements - C# Language Specification | Microsoft Learn § The for statement · retrieved 2026-11-20 · rev 1353149340
Cite this reference
MLA "Statements - C# Language Specification | Microsoft Learn § The for statement." 2026-11-20. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/statements#1394-the-for-statement. Accessed 24 November 2026.
Chicago "Statements - C# Language Specification | Microsoft Learn § The for statement." 2026-11-20. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/statements#1394-the-for-statement.
APA encyclopegia.org. (2026). Statements - C# Language Specification | Microsoft Learn § The for statement. Retrieved November 24, 2026, from https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/statements#1394-the-for-statement
- P1161R2: Deprecate uses of the comma operator in subscripting expressions · retrieved 2022-09-04 · rev 1353149340
Cite this reference
MLA "P1161R2: Deprecate uses of the comma operator in subscripting expressions." 2022-09-04. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1161r2.html. Accessed 24 November 2026.
Chicago "P1161R2: Deprecate uses of the comma operator in subscripting expressions." 2022-09-04. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1161r2.html.
APA encyclopegia.org. (2022). P1161R2: Deprecate uses of the comma operator in subscripting expressions. Retrieved November 24, 2026, from https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1161r2.html
- Multidimensional subscript operator · retrieved 2026-11-20 · rev 1353149340
Cite this reference
MLA "Multidimensional subscript operator." Mark Hoemmen; Daisy Hollman; Corentin Jabot; Isabella Muerte; Christian Trott (2021-09-14). 2026-11-20. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2128r6.pdf. Accessed 24 November 2026.
Chicago "Multidimensional subscript operator." Mark Hoemmen; Daisy Hollman; Corentin Jabot; Isabella Muerte; Christian Trott (2021-09-14). 2026-11-20. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2128r6.pdf.
APA Mark Hoemmen; Daisy Hollman; Corentin Jabot; Isabella Muerte; Christian Trott (2021-09-14). (2026). Multidimensional subscript operator. Retrieved November 24, 2026, from http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2128r6.pdf
- SFINAE · retrieved 2022-08-29 · rev 1353149340
Cite this reference
MLA "SFINAE." 2022-08-29. https://en.cppreference.com/w/cpp/language/sfinae. Accessed 24 November 2026.
Chicago "SFINAE." 2022-08-29. https://en.cppreference.com/w/cpp/language/sfinae.
APA encyclopegia.org. (2022). SFINAE. Retrieved November 24, 2026, from https://en.cppreference.com/w/cpp/language/sfinae
- 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 --)." Wikipedia revision 1353149340, retrieved 2026-11-21 · retrieved 2026-11-20 · rev 1353149340
Cite this reference
MLA "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 --)." Wikipedia revision 1353149340, retrieved 2026-11-21." Effective Go : for , "Finally, Go has no comma operator and ++ and -- are statements not expressions. 2026-11-20. http://golang.org/doc/effective_go.html. Accessed 24 November 2026.
Chicago "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 --)." Wikipedia revision 1353149340, retrieved 2026-11-21." Effective Go : for , "Finally, Go has no comma operator and ++ and -- are statements not expressions. 2026-11-20. http://golang.org/doc/effective_go.html.
APA Effective Go : for , "Finally, Go has no comma operator and ++ and -- are statements not expressions. (2026). 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 --)." Wikipedia revision 1353149340, retrieved 2026-11-21. Retrieved November 24, 2026, from http://golang.org/doc/effective_go.html
- The Go Programming Language Specification: If statements Wikipedia revision 1353149340, retrieved 2026-11-21 · retrieved 2026-11-20 · rev 1353149340
Cite this reference
MLA "The Go Programming Language Specification: If statements Wikipedia revision 1353149340, retrieved 2026-11-21." 2026-11-20. http://golang.org/ref/spec#If_statements. Accessed 24 November 2026.
Chicago "The Go Programming Language Specification: If statements Wikipedia revision 1353149340, retrieved 2026-11-21." 2026-11-20. http://golang.org/ref/spec#If_statements.
APA encyclopegia.org. (2026). The Go Programming Language Specification: If statements Wikipedia revision 1353149340, retrieved 2026-11-21. Retrieved November 24, 2026, from http://golang.org/ref/spec#If_statements
- Other operators · retrieved 2026-11-20 · rev 1353149340
Cite this reference
MLA "Other operators." cppreference. 2026-11-20. https://en.cppreference.com/cpp/language/operator_other. Accessed 24 November 2026.
Chicago "Other operators." cppreference. 2026-11-20. https://en.cppreference.com/cpp/language/operator_other.
APA cppreference. (2026). Other operators. Retrieved November 24, 2026, from https://en.cppreference.com/cpp/language/operator_other
- Perlop - Perl operators and precedence - Perldoc Browser · retrieved 2026-11-20 · rev 1353149340
Cite this reference
MLA "Perlop - Perl operators and precedence - Perldoc Browser." 2026-11-20. https://perldoc.perl.org/perlop.html#Comma-Operator. Accessed 24 November 2026.
Chicago "Perlop - Perl operators and precedence - Perldoc Browser." 2026-11-20. https://perldoc.perl.org/perlop.html#Comma-Operator.
APA encyclopegia.org. (2026). Perlop - Perl operators and precedence - Perldoc Browser. Retrieved November 24, 2026, from https://perldoc.perl.org/perlop.html#Comma-Operator
- Comma operator · retrieved 2026-11-20 · rev 1353149340
Cite this reference
MLA "Comma operator." 2026-11-20. https://web.archive.org/web/20140712200108/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator. Accessed 24 November 2026.
Chicago "Comma operator." 2026-11-20. https://web.archive.org/web/20140712200108/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator.
APA encyclopegia.org. (2026). Comma operator. Retrieved November 24, 2026, from https://web.archive.org/web/20140712200108/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
- Comma Operator · retrieved 2026-11-20 · rev 1353149340
Cite this reference
MLA "Comma Operator." 2026-11-20. https://web.archive.org/web/20190802022035/https://docs.microsoft.com/en-us/cpp/cpp/comma-operator?view=vs-2019. Accessed 24 November 2026.
Chicago "Comma Operator." 2026-11-20. https://web.archive.org/web/20190802022035/https://docs.microsoft.com/en-us/cpp/cpp/comma-operator?view=vs-2019.
APA encyclopegia.org. (2026). Comma Operator. Retrieved November 24, 2026, from https://web.archive.org/web/20190802022035/https://docs.microsoft.com/en-us/cpp/cpp/comma-operator?view=vs-2019
Cite this article
MLA
"comma operator." encyclopegia.org, 2026, https://encyclopegia.org/wiki/comma-operator. Accessed 24 November 2026.
Chicago
"comma operator." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/comma-operator.
APA
encyclopegia.org. (2026). comma operator. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/comma-operator
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON