HPlogo HP C/HP-UX Reference Manual: Workstations and Servers > Chapter 5 Expressions

Comma Operator

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

The comma operator is a binary operator whose operands are expressions. The expression operands are evaluated from left to right.

Syntax

expression ::=
assignment-expression
expression , assignment-expression

Description

The comma operator is a "no-operation" operator. Its left operand is evaluated for its side effects only. Its value is discarded. The right operand is then evaluated and the result of the expression is the value of the right operand.

Because the comma is a punctuator in lists of function arguments, you need to use care within argument lists to ensure that the comma is treated as a comma operator and not as an argument separator.

f(a, (b=7, b), c);

This example passes three arguments to f(). The first is the value of a, the second is the value of b which is set equal to 7 before the function call, and the third is the value of c. The comma separating the assignment expression and the argument b is enclosed in parentheses. It is therefore interpreted as a comma operator and not as an argument separator.

Examples

func(a, (b=0, b), c) /*Set b to 0 before passing it to func. */

index++, a = index /*Increment index and then assign it to a.*/

i=0, j=0, k=0 /*Initialize i,j,k to 0 */