Question

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 ?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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).

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top