Comma operator
Encyclopedia
In the C and C++
programming languages, the comma operator (represented by the token
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.
A handy reminder is that
has the same effect as using C's ternary operator like
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):
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
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 operandOperand
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.
A handy reminder is that
has the same effect as using C's ternary operator like
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):
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 pointSequence pointA 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