user103214
user103214

Reputation: 3658

Confusion with syntax for postfix expressions and operators

In the following case,

int i = 0;
int j = 42;
i = j++;

I know ++ is posfix operator, So, is j a posfix expression or should you sayj++ is posfix expression ?

Upvotes: 0

Views: 262

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263197

Syntactically, both j and j++ are postfix-expressions.

See the grammar in section 5.2 of the C++ 2003 standard:

postfix-expression:
    primary-expression
    ...
    postfix-expression ++

(j is also a primary-expression; j++ is not.)

The fact that a primary-expression is a kind of postfix-expression (even if it doesn't contain a postfix operator) is mostly a matter of convenience for defining the language syntax. There's not much point in referring to j as a postfix-expression unless you're talking about parsing C++ (or C) source code.

Upvotes: 6

Related Questions