Explaining the difference between a statement and an expression in c++

前端 未结 3 1257
日久生厌
日久生厌 2021-01-12 09:39

I am trying to understand thoroughly the difference between a statement and an expression
But i am finding it confusing even after reading this answer

3条回答
  •  灰色年华
    2021-01-12 10:20

    The definition of expression is given in the C Standard (6.5 Expressions)

    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.

    As for expression-statements then they are ended with a semicolon. Here is the definition of the expression statement in C++

    expression-statement:
    expression opt;
    

    And

    An expression statement with the expression missing is called a null statement.

    Relative to the last quote I would like to point to a difference between C and C++. In C++ declarations are statements while in C declarations are not statements. So in C++ you may place a label before a declaration while in C you may not do so. So in C you have to place a null statement before a declaration. Compare

    C++

    Label:
    int x;
    

    C

    Label: ;
    int x;
    

提交回复
热议问题