What is a full expression in C?
Asked Answered
B

2

13

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 ?

Beauteous answered 20/2, 2018 at 7:39 Comment(7)
"Examples of full expressions include the expression in an expression statement"...wat?Maidenhair
What the first phrase means, is that a full expression is maximal, and not a subexpression of another one.Bremerhaven
Well, it is a point without a lot of bearing on your learning C. What he is trying to explain is a "full expression" would be the while (condition) { /* ... stuff ... */ } statement of the loop itself to the best of my read. It isn't a term you should lose sleep over, but do learn to identify the complete extent of a statement, etc.. See C11 - §6.8 Statements and blocksIntuitional
I don't know about the C book, but Stephen Prata is not highly regarded for his C++ Primer Plus book. See e.g .this review for some details.Sensor
It's an abridged - and therefore incorrect - copy from what the standard states. Burn the book.Taut
@Someprogrammerdude I know Stephen Prata book in C++ isn't recommended . But his book in C , in my opinion is highly recommended . I read 5 chapters till now . It has covered many topics in an amazing explanation and nice format . The book covers data types and tricky topics about data and conversion between different types . Till now , Prata really wrote excellent book including tons of tricky details .Beauteous
@Maidenhair There's this thing called an "expression statement". It's an expression, optionally followed by a semicolon, that fulfills the semantic role of a statement. That's saying that when you have an expression statement, the expression in it is a full expression. If you have, say, if (x > 3) x = 2; then x = 2; is an expression statement, hence the expression in it (x = 2) is a full expression.Minny
G
13

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.

Guyguyana answered 20/2, 2018 at 7:51 Comment(2)
Link C11 - §6.8 Statements and blocksIntuitional
@Abd-ElrahmanMohamed Type it in Google and the answer shall be revealed. It is a language feature that was introduced in C 20 years ago - basically it is a local scope, anonymous object. Any decent C programming book will explain it.Guyguyana
F
4

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.

Filberto answered 20/2, 2018 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.