Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Standard - Comma Operator Syntax

according to the C Standard (and K&R) the syntax of the Comma-Operator is as follows:

expression:
    assignment-expression
    expression, assignment-expression

But why does this statement work?

5+5, 1+1; 

5+5 and 1+1 are not assignment-expressions, but the C Standard requires assignment-expressions as operands for the Comma-Operator.

like image 750
Würst Chen Avatar asked Mar 12 '23 21:03

Würst Chen


2 Answers

assignment-expressionconditional-expressionlogical-OR-expressionlogical-AND-expressioninclusive-OR-expressionexclusive-OR-expressionAND-expressionequality-expressionrelational-expressionshift-expressionadditive-expression which finally are ⊃ additive-expression + multiplicative-expression.

So no, 5+5 is indeed ∈ assignment-expression.


⊃ is the "contains" relation.

like image 160
a3f Avatar answered Mar 19 '23 04:03

a3f


The way the C grammar is defined may not be obvious in the first place.

First, let's look how the assignment-expression is defined:

(6.5.16) assignment-expression:
    conditional-expression
    unary-expression assignment-operator assignment-expression

This means that it can be either conditional-expression or the latter combination of tokens. The former is defined as:

(6.5.15) conditional-expression:
    logical-OR-expression
    logical-OR-expression ? expression : conditional-expression

Eventually, you will encounter:

(6.5.7) shift-expression:
    additive-expression
    shift-expression << additive-expression
    shift-expression >> additive-expression

where additive-expression corresponds to expressions such 1+1.

like image 36
Grzegorz Szpetkowski Avatar answered Mar 19 '23 02:03

Grzegorz Szpetkowski