C Expression Evaluation
A series of operators and operands (variables or literals) that produce a value is called an expression. An expression can also refer to a constant, variable, or function call on its own. Expressions evaluAate to a valid value and adhere to the grammatical norms of the language C. A data object, like a number or a letter, is represented by an expression, which can also contain logical conditions and operators. All of the expressions are either L-values or R-values.
Every variable is a L-value, which is an expression that points to a specific location in memory. R-values are temporary objects that don’t last longer than the expression that utilizes them; they can only appear on the right-hand side of an assignment because they will have a value but can’t have one assigned to it. R-values are literals. Whereas constants are R-values (for “right values”) because they can only be used on the right side of an assignment operator, variable names are L-values (for “left values”). It is possible to use a L-value as a R-value, but not the other way around.
Understanding how an arithmetic statement or expression is executed precisely is necessary when it contains two or more operators. The operators’ precedence and associativity rules dictate this.
- Precedence: The priority that is given to several operators is called precedence. It outlines the sequence in which the operands are subjected to the operators. Higher precedence operators are executed before lower precedence operators.
- Associativity: The compiler use associativity to determine whether an operand is grouped with the operator to its left or right when an expression contains operators of equal precedence. Whether operators evaluate from left to right or from right to left is determined by associativity.
For instance, multiplication (*) takes priority over addition (+) in the phrase a = b + c * d;. As a result, c * d is understood to be executed first, followed by the addition of the outcome to b. This can be expressed more clearly as a = b + (c * d);.
The following precedence levels is the from highest to lowest:
- In parenthesis (). First, operations enclosed in parenthesis are assessed. The innermost brackets are assessed first if brackets are nested.
- ++ prefix, — prefix, unary +, unary -,!, ~, * pointer, & address, sizeof, and other unary operators are examples. A lot of unary operators are associative from right to left.
- Operators for multiplication (*, /, %). Generally speaking, these are given preference over additive operators. Their associativity is often left-to-right.
- Operators for addition (+, -). Their associativity is often left-to-right.
- Operators for relationships (<, <=, >, >=). They are usually left-to-right associative and have the same precedence.
- Operators for equality (==,!=). They are usually left-to-right associative and have a lower precedence than relational operators.
- Logical AND (&&). It is more important than logical OR. Left-to-right associativity is the norm.
- OR logically (||). Left-to-right associativity is the norm.
- Operator with conditions (?:). This operator is ternary. Right-to-left associativity is the norm.
- Operators for assignments (=, +=, -=, etc.). Every assignment operator has a precedence that is lower than that of the majority of other operators. They are associative from right to left.
The associativity rule is used if the operators in the expression have the same type or priority.
Example of Expression Evaluation (C)
In this case, x and the intermediate outcomes are integers, and we will evaluate x = 18 – 10 / (2 + 4) * (2 – 1).
- First, assess the expressions enclosed in the innermost parenthesis:
- (2 + 4) equals 6.
- (2 – 1) equals 1. Now, the formula is x = 18 – 10 / 6 * 1;
- Use operators according to precedence. Subtraction (-) is less important than division (/) and multiplication (*). Make use of associativity (left-to-right for / and *) since they have the same precedence:
- When 10 is divided by 6, the result is 1. Now, the formula is x = 18 – 1 * 1;
- 1 * 1 equals 1. Now, the formula is x = 18 – 1;
- Use the subtraction symbol (-):
- 18 minus 1 equals 17. Now, the formula is x = 17;
- Use Assignment (=). Compared to arithmetic operators, it has a lower precedence:
- x = 17; gives x the value 17.
17 is the ultimate value of x.