Comma Operator

suggest change

The comma operator evaluates its left operand, discards that value, and then evaluates its right operand. The result is the value of the right operand. There is a sequence point between the two operands.

int x = 42, y = 42;
printf("%i\n", (x *= 2, y)); /* Outputs "42". */

The comma used to separate function arguments is not the comma operator. It is a separator, and it does not introduce a sequence point between arguments. Argument evaluation order is unsequenced.

The above printf() call contains both the comma operator and the separator.

printf("%i\n", (x *= 2, y)); /* Outputs "42". */
/*           ^        ^ this is a comma operator */
/*           this is a separator */

The comma operator is often used in the initialization section as well as in the updating section of a for loop. For example:

/* Prints the odd numbers less than 10. */
for (k = 1; k < 10; printf("%d\n", k), k += 2)
    ;

/* Prints each k from 1 through 9 and the sum 1 + ... + k. */
for (sumk = 1, k = 1; k < 10; k++, sumk += k)
    printf("%5d%5d\n", k, sumk);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents