Flipkart.com

I.11: What is an rvalue?

Answer:
In I.9, an lvalue was defined as an expression to which a value can be assigned. It was also explained that an lvalue appears on the left side of an assignment statement. Therefore, an rvalue can be defined as an expression that can be assigned to an lvalue. The rvalue appears on the right side of an assignment statement.
Unlike an lvalue, an rvalue can be a constant or an expression, as shown here:

int x, y;
x = 1; /* 1 is an rvalue; x is an lvalue */
y = (x + 1); /* (x + 1) is an rvalue; y is an lvalue */

As stated in I.9, an assignment statement must have both an lvalue and an rvalue. Therefore, the following statement would not compile because it is missing an rvalue:

int x;
x = void_function_call() /* the function void_function_call() returns nothing */
If the function had returned an integer, it would be considered an rvalue because it evaluates into something
that the lvalue, x, can store.
Reference:
I.9: What is an lvalue?
I.10: Can an array be an lvalue?


I.12: Is left-to-right or right-to-left order guaranteed for operator precedence?
Answer:
The simple answer to this question is neither. The C language does not always evaluate left-to-right or right to- left. Generally, function calls are evaluated first, followed by complex expressions and then simple expressions. Additionally, most of today’s popular C compilers often rearrange the order in which the expression is evaluated in order to get better optimized code. You therefore should always implicitly define
your operator precedence by using parentheses.
For example, consider the following expression:

a = b + c/d / function_call() * 5

The way this expression is to be evaluated is totally ambiguous, and you probably will not get the results you want. Instead, try writing it by using implicit operator precedence:
a = b + (((c/d) / function_call()) * 5)

Using this method, you can be assured that your expression will be evaluated properly and that the compiler will not rearrange operators for optimization purposes.

No comments:

Post a Comment