Operatore virgola

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

Uses your system voice; voice and rate in settings.

The name shown is in it. The article below is in English and is not a translation.

≈ 8 min read

Cite, export and learn8 citation formats · 8 exports · lesson and quiz

Names in other languages

  • ItalianoOperatore virgola
  • СрпскиОперација зарез
  • Englishcomma operator
  • eokomo-operatoro
  • فارسیعملگر کاما
  • 日本語コンマ演算子順次演算子, 順次評価演算子
  • 繁體中文逗號運運算元
  • 中文逗号运算符
  • 中文逗号运算符
  • 简体中文逗号运算符

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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 operator is distinct from its use in function calls and definitions, variable declarations, enum declarations, and similar constructs, where it acts as a separator.

  • 4 Other languages
  • 5 See also
  • 6 References
  • 7 External links
  • Syntax

    edit edit source

    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:[1] (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.[2] In C++23, the ability to overload operator[] with multiple arguments was added making unparenthesised comma expressions unusable in subscripts.[3] 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

    edit edit source
    Example Return value Behavior
    int a = 1, b = 2; int r = (a, b); return r; 2 During initialization the comma acts as a separator, but in the second line it is an operator. The parentheses are required. Without them the second line would be equivalent to int r = a; int b, which results in b being defined twice, which is illegal.
    int a = 1, b = 2; int r = (a += 2, a + b); return r; 5 since a += 2 is evaluated before a + b is evaluated and assigned to r.
    return(1, 2); 2 The comma is an operator, not a separator, since return is a keyword and does not act as a function.

    Uses

    edit edit source

    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.[4] 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

    edit edit source

    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.[5]

    Macros

    edit edit source

    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 <= 9; i++) { assert(("i is too big!", i <= 4)); printf("i = %i\n", i); }

    Condition

    edit edit source

    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.[6]

    Complex return

    edit edit source

    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

    edit edit source

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

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

    Overloading

    edit edit source

    C++ allows overloading the comma operator, as both a binary operator and as a non-member.[7] 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

    edit edit source

    In the OCaml and Ruby programming languages, the semicolon (";") is used for this purpose.

    JavaScript,[8] Perl[9] and D[10] utilize the comma operator in the same way C/C++ does.

    Some other languages with C-like syntax, such as Java[11] and C#, do not have this feature, though C# supports a comma-separated list of expressions in the initializer and iterator of a for statement,[12] thus enabling an equivalent of this in this specific context.

    See also

    edit edit source

    References

    edit edit source
    1. ^ "Comma Operator". Microsoft dev docs. Archived from the original on 2 August 2019. Retrieved 1 August 2019. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated. Wikipedia revision 1353149340, retrieved 2026-11-21
    2. ^ "P1161R2: Deprecate uses of the comma operator in subscripting expressions". www.open-std.org. Retrieved 2022-09-05. Wikipedia revision 1353149340, retrieved 2026-11-21
    3. ^ Mark Hoemmen; Daisy Hollman; Corentin Jabot; Isabella Muerte; Christian Trott (2021-09-14). "Multidimensional subscript operator" (PDF). Wikipedia revision 1353149340, retrieved 2026-11-21
    4. ^ "SFINAE". en.cppreference.com. Retrieved 2022-08-30. Wikipedia revision 1353149340, retrieved 2026-11-21
    5. ^ 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
    6. ^ The Go Programming Language Specification: If statements Wikipedia revision 1353149340, retrieved 2026-11-21
    7. ^ cppreference.com. "Other operators". cppreference.com. cppreference.com. Retrieved 2 May 2026. Wikipedia revision 1353149340, retrieved 2026-11-21
    8. ^ "Comma operator". MDN Web Docs. 17 January 2020. Archived from the original on 12 July 2014. Retrieved 25 January 2020. You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. Wikipedia revision 1353149340, retrieved 2026-11-21
    9. ^ "Perlop - Perl operators and precedence - Perldoc Browser". Wikipedia revision 1353149340, retrieved 2026-11-21
    10. ^ "Expressions - D Programming Language § Comma Expression". Wikipedia revision 1353149340, retrieved 2026-11-21
    11. ^ "Is comma (,) operator or separator in Java?". Wikipedia revision 1353149340, retrieved 2026-11-21
    12. ^ "Statements - C# Language Specification | Microsoft Learn § The for statement". Wikipedia revision 1353149340, retrieved 2026-11-21

    Bibliography

    edit edit source
    • 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.; Ritchie, Dennis M. (1988), The C Programming Language (2nd ed.), Englewood Cliffs, NJ: Prentice Hall
    edit edit source

    Text derived from https://en.wikipedia.org/w/index.php?oldid=1353149340 under CC BY-SA 4.0; modifications have been made.

    Words in each sectionUsesUsesUses: 551 words551SyntaxSyntaxSyntax: 247 words247ExamplesExamplesExamples: 116 words116Other languagesOther languagesOther languages: 90 words90See alsoSee alsoSee also: 8 words8
    Words in each section. Measured from the stored article body; sections below the minimum are not plotted.

    Provenance

    snapshot comma-operator-587070-dd4bb01f330ece0e · hash dd4bb01f330ece0ecc78aea7d88b750348ca33521afd5e7af787adeb5491f6c1 · captured 2026-05-08 · ledger · version history

    Cite this page as: encyclopegia.org, “Operatore virgola”, snapshot comma-operator-587070-dd4bb01f330ece0e. https://encyclopegia.org/a/comma-operator