Expressions:
It is defined as the combination of operands and operators where operand can be a variables or constants or direct values.
Example: x=a*(b/c)%d;
Example: x=a*(b/c)%d;
Types of Expressions: An Expression can be classified as specified below.
Type Example
Arithmetic Expression c=a+b;
Arithmetic Expression c=a+b;
Relational Expression a>b;
Logical Expression (a<10) && (b>20)
Assignment Expression c=b;
Bitwise Expression a<<2;
Conditional Expression etc. (a<b) ? c=0 :c=1;
Evaluation of Expressions:
Expressions are evaluated using an assignment statement of the form shown below variable = expression;
When an assignment statement is encountered, the right hand side expression is evaluated first and then replaces the value of the variable on the left hand side. Note that, all variables used in the expression must be assigned values before evaluation is attempted.
Example:
let a=9; b=12; and c=3, then
let a=9; b=12; and c=3, then
x=a*b-c;
x=9*12-3;
x=9*9 = 81;
Rules of evaluation of Expressions:
1. When parentheses are used in the expression, then the expression with in parenthesis assumes highest
priority and it should be evaluated first.
2. If parentheses are nested, then expression evaluation begins with inner most sub expression.
3. Apply operator precedence rules, if more operators of same type are in the expression.
4. Apply associativity of operators, if more operators of same precedence are in the expression.
To have a complete understanding of how expressions are evaluated one should know about the following things:
• Operator Precedence
• Associativity of operators
• Type Conversion
Note: The operator precedence rules and associativity of operators can be changed by introducing parenthesis in the expression. We know that, parenthesized expression assumes highest priority in evaluation process.
Precedence and Associativity of Operators:
Example:
Consider the expression 5 * 4 + 8 / 2
In the above expression we have two operators with the same precedence i.e., * and / so we apply associativity first i.e., we evaluate the expression from left to right based on the above table. Next we consider the first occurrence of the operator with that precedence and evaluate the operands placed before and after that operator for the operation. i.e.,5 * 4.
Type conversion:
The ‘C’ language provides a facility of mixing different type of variables and constants in expression. But while evaluating those expressions, it is needed to convert the data type one variable may be converted to another data type. This is called as “Type conversion.” There are two types of conversions. They are
1. Implicit (Automatic) type conversion. 2. Explicit type conversion (Type Casting).
(i) Implicit (Automatic) type conversion:
In this conversion, the user or programmer does not provide any statements to perform conversion process. The compiler automatically converts data type of variables one type to another. This is called as Implicit type conversion.
Normally, in the expression if operands are of different types, then lower data type value is converted into higher data type and the evaluation process proceeds.
Example:
let int x; float a=10.5; int b=2;
let int x; float a=10.5; int b=2;
a ->float
x= ---
b-> int-> float (Conversion)
10.5->float 10.5
x= ------ x = -------- = 5.25-> float ->int = 5.
2 -> int ->float 2.0
(ii) Explicit type conversion (Type Casting):
In this conversion, the user or programmer forcibly provides the statements to perform conversion process. The programmer uses the cast operator to convert one type of data to another type by specifying the data type required. This is called as Type casting or Explicit type casting.
Syntax: variable = (data_type) expression/variable.
Example:
x= (int) 7.5 =7;
x= (int) 7.5 =7;
b= (double) sum/x;