C Programming - Expressions Types
C Programming - Expressions Types
In this tutorial you will learn about Expressin in C programming language - Arithmetic
Expressions, Evaluation of Expressions, Precedence in Arithmetic Operators, Rules for
evaluation of expression, Type conversions in expressions, Implicit type conversion, Explicit
Conversion and Operator precedence and associativity,
Arithmetic Expressions
Algebraic C Expression
Expression
axb–c a*b–c
(m + n) (x + y) (m + n) * (x + y)
(ab / c) a*b/c
(x / y) + c x/y+c
Evaluation of Expressions
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.
.
main ()
{
float a, b, c x, y, z;
a = 9;
b = 12;
c = 3;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * (2 – 1);
z = a – ( b / (3 + c) * 2) – 1;
printf (“x = %fn”,x);
printf (“y = %fn”,y);
printf (“z = %fn”,z);
}
.
output
x = 10.00
y = 7.00
z = 4.00
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 + -
During evaluation it adheres to very strict rules and type conversion. If the operands are of
different types the lower type is automatically converted to the higher type before the operation
proceeds. The result is of higher type.
Explicit Conversion
Many times there may arise a situation where we want to force a type conversion in a way that is
different from automatic conversion.
Consider for example the calculation of number of female and male students in a class
........................female_students
Ratio =........-------------------
........................male_students
Since if female_students and male_students are declared as integers, the decimal part will be
rounded off and its ratio will represent a wrong figure. This problem can be solved by converting
locally one of the variables to the floating point as shown below.
The operator float converts the female_students to floating point for the purpose of evaluation of
the expression. Then using the rule of automatic conversion, the division is performed by
floating point mode, thus retaining the fractional part of the result. The process of such a local
conversion is known as explicit conversion or casting a value. The general form is
(type_name) expression
Each operator in C has a precedence associated with it. The precedence is used to determine how
an expression involving more than one operator is evaluated. There are distinct levels of
precedence and an operator may belong to one of these levels. The operators of higher
precedence are evaluated first.
The operators of same precedence are evaluated from right to left or from left to right depending
on the level. This is known as associativity property of an operator.