C Language Operators Comma Operator

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Evaluates its left operand, discards the resulting value, and then evaluates its rights operand and result yields the value of its rightmost operand.

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

The comma operator introduces a sequence point between its operands.

Note that the comma used in functions calls that separate arguments is NOT the comma operator, rather it's called a separator which is different from the comma operator. Hence, it doesn't have the properties of the comma operator.

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:

for(k = 1; k < 10; printf("\%d\\n", k), k += 2);   /*outputs the odd numbers below 9/*

/* outputs sum to first 9 natural numbers */
for(sumk = 1, k = 1; k < 10; k++, sumk += k)
    printf("\%5d\%5d\\n", k, sumk);


Got any C Language Question?