What is a sub-expression in C?
Asked Answered
A

4

6

What is sub expression in C? I thought combination of smaller expression is subexpression eg: a*(b+C/d)/20

b+c/d is subexpression is it correct? or alone c/d is subexpression ?

Anora answered 22/3, 2014 at 12:36 Comment(0)
C
8

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.

In summary, a sub-expression is an argument to an operator or function, and such an argument expression can itself have sub-expressions.


Regarding your example

a*(b+C/d)/20

and your concrete questions

b+c/d is subexpression is it correct? or alone c/d is subexpression ?

Yes, and yes (modulo uppercase/lowercase typo).

However, for example, here b+C is not a sub-expression.

Campy answered 22/3, 2014 at 12:52 Comment(2)
Trying to understand why b+C is not a sub-expression. Is it not an argument that is passed to the division operator?Art
@codingManiac: the expression b+C/d is parsed as b+(C/d), due to the grammar of C++. A simplified way to view that is that / has higher precedence than +, and you can find operator precedence tables everywhere. However, it's worth noting that the C++ standard only specifies the grammar, from which precedence is deduced, and that the simpler precedence view breaks down in certain situations, e.g. for the ?: choice operator. The only common programming language I know that doesn't effectively have precedence for arithmetic operators, is old Smalltalk.Campy
S
4

what is sub expression in c.i thought combination of smaller expression is subexpression

Yes. You thought correct.
A sub-expression is a part of an expression that is by itself a correct expression.

Sometimes a sub-expression is a constant, like 20 here. /20 is not a correct expression itself hence it can't be a sub-expression.
In general:

a subexpression is a part of an expression that corresponds to a subtree in the parse tree -- that is, some node in the parse tree plus all of its descendants. The subexpression is a proper subexpression if it is not the entire expression.

To answer your specific questions:

b+c/d is sub-expression is it correct?

Yes.

or alone c/d is subexpression ?

Yes. a, b, C, d, 20, b+C/d and a*(b+C/d) are all sub-expressions.

Suborn answered 22/3, 2014 at 12:38 Comment(2)
In a well-known quirk of the C grammar, in the constant -20, 20 is a subexpression.Dipteran
@PascalCuoq; Added a new, more general, definition for you :0. BTW, I hope - sign is unary - here.Suborn
W
4

A sub expression is any smaller unit in an expression, so C/d and b+C/d both are sub-expressions,

Whereas a unit here is a combination of two operands and an operator in between.
Example,

C/d is a unit, and so is (b+C/d)
note that in (b+C/d), C/d will be executed first after that (b+C/d) will be a unit.

Also, a*(b+C/d) is a sub-expression.

and adding /20 to the rest of the expression will be complete expression, so that will not be a sub-expression.

In other words we can say that in an expression, all smaller expressions contained in the complete expression are sub-expressions except the expression executed in the last(order of execution depends upon the priorities of operators).

Wilda answered 22/3, 2014 at 12:39 Comment(7)
I think you mean “smaller”. Using “smallest” makes it appear as you are saying the opposite of what you are trying to say.Dipteran
Note: /d)/20 is not a subexpression.Veliger
@GrijeshChauhan Yes because that will be evaluated in the last and that will also be in the last part of the expression, and if added will be the whole expression.Wilda
"any smaller unit" might make sense, but in order for it to make sense the term "unit" must be explained (otherwise it's full circularity)Campy
@Cheersandhth.-Alf I have edited my answer please suggest any more editions if think they are necessary.Wilda
@AnkurKumawat: Well the word "here" in the new text means that it's an explanation by example, and that's fine, as I see it. It probably won't hurt to add a "more in general", about e.g. unary operators and function calls. But it may not necessarily be more clear either.Campy
@Cheersandhth.-Alf thanks for your suggestion, i have improved my answer accordingly.Wilda
M
1

An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects. [§5.1]

So, b+c/d and c/d both are expressions. Even, a,b,c,d and 20 are expressions.

Mobley answered 22/3, 2014 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.