Saturday, January 7, 2012

Is an arithmetic expression a complete C statement and how are arithmentic expressions used in assignment statements?

An arithmetic expression is not a complete C statement, but only a component of a statement. The value evaluated from the expression may be stored in a variable using an assignment statement. For example, the arithmetic expression x / y is part of a C assignment statement

d = x / y;

The statement assigns the value obtained from the arithmetic expression on the right to the variable on the left. Thus, the assignment statement

i = i + 1

although not looking correct algebraically, is a valid C assignment. The arithmetic expression i + 1 creates a new value that is 1 greater than the current value of i. The assignment statement then gives i this new value.

Note that we cannot write these two assignment statements as

x / y = d;
i + 1 = i;

because on the left side of assignment statements we can have only single variables, not expressions. Single variables are allowed to be lvalues (pronounced "ell-values"), meaning they are allowed to be on the left side of assignment statements. Expressions are rvalues (pronounced "are-values") because they are allowed on the right side of assignment statements.

No comments:

Post a Comment