Tuesday, October 22, 2013

Identifiers, Integer and float conversions

Identifiers:
In computer programming language, the names of variables, functions, arrays and other objects are of prime importance. The names of variables, functions, arrays and other objects are called as identifiers.

Rules for constructiong identifiers:
All rules applied to the variables are applied to the identifiers as well.

Integer and float conversions
The following rules are used for implicit conversion of floating point and integer values in C.

♦ If both the operands are of type int then the result is of type int (considering the resultant variable is of type int).

♦ If either operand or both, is of type float then the result is of type float (considering the resultant variable is of type float).

♦ If the expression evaluates of type int and the resultant variable of type float then the int will be converted to an equivalent float before assignment to the resultant variable.

♦ If the expression evaluates of type float and the resultant variable of type int then the float will be converted to an int, usually by rounding towards zero, before assignment to the resultant variable.
It is quite to loose accuracy in an assignment statement.
E.g.
          i=3.5;

♦ If the variable i is declared as an int then the compiler will convert the value 3.5 to an integer value before carrying out the assignment. hence the value 3 will be assigned to the variable i. The compiler will normally truncate the float value to the integer value, which is nearer to zero. Rounding to the nearest integer is not carried out.

♦ The result of the division operator between two int operand is of type int. It gives the result truncated towards zero if the result is positive and the language does not define what should happen if the result is negative.
E.g. 2/5=0 and

      -2/5=0 also.

No comments:

Post a Comment