Comma operator
Encyclopedia
In the C and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand
Operand
In mathematics, an operand is the object of a mathematical operation, a quantity on which an operation is performed.-Example :The following arithmetic expression shows an example of operators and operands:3 + 6 = 9\;...

 and discards the result, and then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence
Precedence
Precedence may refer to:* Message precedence of military communications traffic* Order of precedence, the ceremonial hierarchy within a nation or state* Order of operations, in mathematics and computer programming...

 of any C operator, and acts as a sequence point
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 yet been performed...

.

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
Side effect (computer science)
In computer science, a function or expression is said to have a side effect if, in addition to returning a value, it also modifies some state or has an observable interaction with calling functions or the outside world...

, such as in the initialiser or the counting expression of a for loop
For loop
In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement....

. For example, the following terse linked list
Linked list
In computer science, a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a datum and a reference to the next node in the sequence; more complex variants add additional links...

 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
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

 programming languages, the semicolon (";") is used for this purpose. JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

 utilizes the comma operator in the same way C/C++ does.

See also
  • Sequence point
    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 yet been performed...

  • Operators in C and C++
    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...

  • Operator (programming)
    Operator (programming)
    Programming languages typically support a set of operators: operations which differ from the language's functions in calling syntax and/or argument passing mode. Common examples that differ by syntax are mathematical arithmetic operations, e.g...

  • Facility from the Boost library which makes it easy to fill containers using comma-operator
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK