An Expression represents data items such as variables, constants, and are interconnected with operators as per the syntax of the language. An expression is evaluated using the assignment operator of the C language.
Syntax: variable = expression
Description: Any valid variable and expression
Example: x = a*b-c
In the above statement, the expression is evaluated first from left to right. After the evaluation of the expression, the final value is assigned to the variable from right to left.
Example: y = (a/b) + c;
z = (a*b) – c;
The blank spaces around an operator are optional and add only to improve the readability of the program. The variables that are used in an expression need to declare at the declaration part of the program.
Example of Algebric expression and C language expression
|ALGEBRIC EXPRESSION||C EXPRESSION|
|a + b x c||a + b * c|
|ax2 + bx + c||a * x * x + b * x + c|
|(4ac/2a)||(4 * a *c / 2 *a)|
|(2x2 / b) – c||(2 * x * x) /b) – c|
Operator Precedence in C language
In C programming language Precedence is used to determine the order in which different operators in a complex expression are evaluated.
Arithmetic operators Precedence
Usually, the Arithmetic operators are evaluated from left to right using the precedence when the expression is written without the parameters.
The Arithmetic Operator precedence
|High||* / %|
result = x – y/3 + z * 3 – 1
x = 3, y = 9, z =77
sum = 3 – 9/3 + 77 * 3 – 1
during the evaluation of the expression, the order of evaluation can be changed by putting the parenthesis in an expression.
(3 – 9) / 3 + 77 * (3 – 1)
– 6/3 + 77 * (3 – 1)
– 6/3 + 77 * (3 -1)
-2 + 154
Rules for evaluation of an expression in c language
- Evaluate the sub-expressions from left to right if parenthesized.
- Evaluate the arithmetic expression from left to right using the rules of precedence.
- The highest precedence is given to the expressions within parenthesis.
- Apply the associative rule, if more operators of the same precedence occur.
- Evaluate the innermost sub-expression if the parenthesis is nested.
Every operator in the C programming language has precedence associated with it. The precedence is used to determine how an expression involving more than one operator is evaluated.
The operators at the higher level of precedence are evaluated first. The operators of the same precedence evaluated either from left to right or right to left depending on the level is known as associativity.
The hierarchy of operators in ‘C’ are summarised below
- Any expression within parenthesis is first evaluated if more than one pair of parenthesis are present, the innermost parenthesis is evaluated first.
- Unary operators are evaluated first in an expression.
- Then priority is given to multiplication and expression.
- Then subtraction and addition are performed.
- Then relational operations are performed.
- Then quality checking is performed.
- Then logical operations are performed.
- Then the operations are checked.
- Finally, the assignment operation is carried out.