Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Comma operator

The `,` operator evaluates the left expression before the right, discards the left value, and yields the right result.

Version: C23; repository compiler flag, production runtime not attested

Syntax

left_expression, right_expression

The , operator evaluates its left operand, discarding its value, then its right. A sequence point separates them: left-side effects finish before right-side evaluation. The result has the right operand’s type and value but is not an lvalue.

Commas separating arguments or initializers are not this operator. In f(a, b) the comma separates arguments; in f((a, b)) the operator forms one argument. A separator alone does not guarantee argument evaluation order.

The operator has the lowest precedence and associates leftward. Use parentheses for intended grouping. In the example, (x += 2, x * 4) first sets x to 3, then yields 12.

Examples

#include <stdio.h>

int main(void)
{
    int x = 1;
    int result = (x += 2, x * 4);
    printf("x=%d result=%d\n", x, result);
    return 0;
}

Result

x=3 result=12

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources