Expression evaluation, operator precedence, associativity
Expression: -
- An expression is a sequence of one or more operands (like variables, constants, and function calls) and operators that the compiler evaluates to produce a single value.
Examples of Expressions in C:
Constant expression : 45
Variable expression :
x
Arithmetic expression :
a + b * c
Assignment expression :
x = 10
Logical expression :
(x > 5)
&& (y < 10)
Complex expression :
(a = b + 5) *
(c = d - 3)
Function call expression :
printf("Hello")
Arithmetic Expressions: -
- An arithmetic expression is a sequence of numeric operands and arithmetic operators that the compiler evaluates to produce a single numerical value.
- It performs mathematical calculations.
- The arithmetic operators are addition (+), subtraction (-), multiplication (*), division (/) and modulus (%).
- Arithmetic expressions follow operator precedence and associativity rules.
- We can use parentheses ( ) to override default precedence.
Note:
- The % operator works only with integer operands.
- / Performs integer division if both operands are integers; otherwise, it performs floating-point division.
Example: -
Output: -
Expression evaluation: -
- Expression evaluation is the process of executing an expression step by step according to the rules of operator precedence and associativity to obtain a final single value.
- Every expression in C is evaluated by:
- Operands (constants, variables, function results).
- Operators (+, -, *, /, %, etc.).
- Rules of precedence & associativity.
Rules for Evaluation:
- Parentheses have the highest priority → expressions inside ( ) are evaluated first.
- Unary operators (++, --, !, etc.) are evaluated next.
- Multiplication, Division, Modulus (*, /, %) are evaluated before Addition/Subtraction.
- Addition, Subtraction (+, -) are next.
- Relational and logical operators come after arithmetic.
- Assignment (=) is done last.
Example: -
Output: -
Operator precedence: -
- Operator precedence is a set of rules that determines the priority order in which different operators in an expression are evaluated.
- Operators with higher precedence are evaluated before those with lower precedence.
- If two operators have the same precedence, then their associativity (left-to-right or right-to-left) decides the order of evaluation.
Associativity: -
- Associativity is a rule that determines the order or direction of evaluation in which operators of the same precedence are evaluated in an expression.
- It acts as the "tie-breaker" when precedence alone isn't enough.
- There are two types of associativity:
1) Left-to-Right: Evaluation
proceeds from the leftmost operation to the right.
- Most operators (like +, -, *, /, %, relational, logical) are evaluated from left to right.
Example: -
Output: -
2) Right-to-Left: Evaluation
proceeds from the rightmost operation to the left.
- Some operators (like assignment =, compound assignment +=, -=, and unary operators ++, --, !) are evaluated from right to left.
Example: -
Output: -