Expressions
An expression is a combination of variables,
constants and operators written according to the syntax of C
language. In C, every expression evaluates
to a value i.e., every expression
results in some value of a certain type that can be assigned to a variable.
Some examples of C expressions are
shown in the table given below.
Algebraic Expression
|
C Expression
|
a
x b – c
|
a
* b – c
|
(m
+ n) (x + y)
|
(m
+ n) * (x + y)
|
(ab
/ c)
|
a
* b / c
|
3x2 +2x + 1
|
3*x*x+2*x+1
|
b2-4ac
|
b*b-4*a*c
|
Evaluation of Expressions
Expressions are evaluated using an assignment
statement of the form
Variable = expression;
Variable is any valid C variable name.
When the statement is encountered, the expression is evaluated first and then replaces
the previous value of the variable on the left hand side. All variables used in
the expression must be assigned
values before evaluation is attempted.
Eg: -
x = a * b – c
y = b / c * a
z = a – b / c + d;
Eg: -
x = a * b – c
y = b / c * a
z = a – b / c + d;
Precedence in Arithmetic Operators
An arithmetic expression without parenthesis will be evaluated
from left to right using the rules of precedence of operators.
There are two
distinct priority levels of arithmetic operators in C.
High priority * / %
Low priority + -
High priority * / %
Low priority + -
Rules for evaluation of expression
1. First parenthesized sub expression left to right are evaluated.
2. If parenthesis are nested, the evaluation begins with the innermost sub expression.
3. The precedence rule is applied in determining the order of application of operators in
2. If parenthesis are nested, the evaluation begins with the innermost sub expression.
3. The precedence rule is applied in determining the order of application of operators in
evaluating
sub expressions.
4. The associability rule is applied when two or more operators of the same precedence level
4. The associability rule is applied when two or more operators of the same precedence level
appear
in the sub expression.
5. Arithmetic expressions are evaluated from left to right using the rules of precedence.
6. When Parenthesis are used, the expressions within parenthesis assume highest priority.
5. Arithmetic expressions are evaluated from left to right using the rules of precedence.
6. When Parenthesis are used, the expressions within parenthesis assume highest priority.
No comments:
Post a Comment