What is a full expression in C?

 ̄綄美尐妖づ 提交于 2021-02-07 05:21:52

问题


I study C language from "C Primer Plus" book by Stephen Prata and it came to the point :

"A full expression is one that’s not a subexpression of a larger expression.Examples of full expressions include the expression in an expression statement and the expression serving as a test condition for a while loop"

I can't understand clearly what is the exact definition of full expressions and why the book considers test conditions are full expressions.

Could any one explain clearly what is meant by "Full Expression" and how can I specify that an expression is full expression or not ?


回答1:


He's taken this straight from the C standard, example C11 6.8:

A full expression is an expression that is not part of another expression or of a declarator. Each of the following is a full expression: an initializer that is not part of a compound literal; the expression in an expression statement; the controlling expression of a selection statement (if or switch); the controlling expression of a while or do statement; each of the (optional) expressions of a for statement; the (optional) expression in a return statement. There is a sequence point between the evaluation of a full expression and the evaluation of the next full expression to be evaluated.

Some examples would be:

if(x)

for(x; y; z)

return x;

where x y and z are full expressions.

Full expression is a language grammar term and not really something a C programmer needs to know about. It is only relevant to "language lawyers" and those making compilers or static analysers. The C standard speaks of statements, blocks and full expressions.

What the programmer might need to know is the last sentences of the above cited text, which means that after a full expression, all side effects of that expression are carried out. So if I write code such as if(i++) printf("%d", i); then I know that the i++ has been carried out before the printf line.

It may however be quite useful to know these dry grammar terms when reading compiler errors. Such as the infamous "statement missing", that most likely means, in plain English, that you've forgotten a semicolon.




回答2:


An operator along with its operands constitute a simple expression which is called the full expression.

A compound expression can be formed by using simpler expressions as operands of the different types of operators. The evaluation order of the operators in an expression will be determined by the operator precedence rules followed in the C language.

A sub-expression is not just any part of a larger expression.

Consider:

2 * 3 + 4 * 5

Here 3+4*5 is not a sub-expression.

The full expression parses as

(2 * 3) + (4 * 5)

and so the direct sub-expressions are 2*3 and 4*5.

Each of those again parse as compositions of smaller things, with 2*3 composed of the sub-expressions 2 and 3, and with 4*5 composed of the sub-expressions 4 and 5.

These sub-expressions of sub-expressions are indirect sub-expressions of the original full expression, so that in total it has these sub-expressions: 2*3, 4*5, 2, 3, 4 and 5.

While e.g. 3+4*5 is not a sub-expression.




回答3:


Consider the statement below

a = b + c;

a = b + c is an expression which is not a subexpression of any larger expression. This is called full expression.
b + c is a subexpression of the larger expression a = b + c therefore it is not a full expression. b is also a subexpression for the full expression a = b + c and subexpression b + c.



来源:https://stackoverflow.com/questions/48880037/what-is-a-full-expression-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!