coperatorsexpressionassignment-operatorstatements

Expressions in C. Are assignments expressions despite semicolons?


I am currently learning C and in one slide the professor states that Semicolons added to an expression makes it a statement. While on the other hand it mentioned that all assignments are expressions. Even if those assignments take extra lines and has to be seperated by semicolons.

See here:

    int a,b,c;
    a = b = c = 10;
    int a,b,c;
    b = 2; 
    c = 3;
    a = b + c;

Would these be considered all expressions? or statements

Tried looking through resources and got conflicting answers


Solution

  • C11 Standard 6.5.1 An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. The value computations of the operands of an operator are sequenced before the value computation of the result of the operator.

    C11 Standard 6.8.2 A statement specifies an action to be performed. Except as indicated, statements are executed in sequence.

    So in a = b = c = 42; we have 3 assignment expressions and 1 statement.
    The expressions are a = <something>, b = <something>, and c = 42. (cf C11 6.5.16)
    The statement is equivalent to the English sentence "Set a, b, and c to 42."