Comma operator

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). The comma operator has the lowest precedence of any C operator, and acts as a sequence point.

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.

In this example, the differing behavior between the second and third lines is due to the comma operator having lower precedence than assignment.

int a=1, b=2, c=3, i;   // comma acts as separator in this line, not as an operator
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 = 5 into i   ... 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

A handy reminder is that

(a, b)

has the same effect as using C's ternary operator like

(a ? b : b)

Because the comma operator discards its first operand, it is generally only useful where the first operand has desirable side effects, such as in the initialiser or the counting expression of a for loop. For example, the following terse linked list cycle detection algorithm (a version of Floyd's "tortoise and hare" algorithm):

bool loops(List *list)
{
    List *tortoise, *hare; /* advance hare 2 times faster than tortoise */
    for (tortoise = hare = list;
                 hare && (hare = hare->next); /* tests for valid pointers + one step of hare */
                 tortoise = tortoise->next, hare = hare->next) /* comma separates hare and tortoise step */
        if (tortoise == hare) /* loop found */
            return true;
    return false;
}

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



Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Comma — For other uses, see Comma (disambiguation). , Comma Punctuation apostrophe …   Wikipedia

  • Comma (disambiguation) — A comma is a type of punctuation mark (,). The word comes from the Greek komma (κόμμα), which means something cut off or a short clause. Comma may also refer to: Comma (music), a type of interval in music theory Comma butterfly, the brush footed… …   Wikipedia

  • Comma (punctuation) — A comma ( , ) is a punctuation mark. It has the same shape as an apostrophe or single closing quotation mark in many typefaces, but it differs from them in being placed on the baseline of the text. Some typefaces render it as a small line,… …   Wikipedia

  • Comma category — In mathematics, a comma category (a special case being a slice category) is a construction in category theory. It provides another way of looking at morphisms: instead of simply relating objects of a category to one another, morphisms become… …   Wikipedia

  • Fat comma — is a programming term primarily associated with the Perl and Ruby programming languages, though others doubtless also employ the token. It refers to an operator, indicated as => , that can be used as a substitute for the comma operator and is an… …   Wikipedia

  • Operators in C and C++ — This is a list of operators in the C and C++ programming languages. All the operators listed exist in C++; the fourth column Included in C , dictates whether an operator is also present in C. Note that C does not support operator overloading.… …   Wikipedia

  • Comparison of Pascal and C — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Sequence point — A sequence point in imperative programming defines any point in a computer program s execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have… …   Wikipedia

  • Text Executive Programming Language — In 1979, Honeywell Information Systems announced a new programming language for their time sharing service named TEX, an acronym for the Text Executive processor. TEX was a first generation scripting language, developed around the time of AWK and …   Wikipedia

  • Order of operations — In mathematics and computer programming, the order of operations (sometimes called operator precedence) is a rule used to clarify unambiguously which procedures should be performed first in a given mathematical expression. For example, in… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”