C Expressions

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. Previous problem can be solved by converting locally one of the variables to the floating point as shown below.
Ratio = (float) female_students / male_students
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


Operator precedence and associatively

Each operator in C has 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.

Make Comments..!!


Oops!! No posts from user.

Download Android App