C allows programmers to do typecasting by placing the type name in parentheses and placing this in front of the value programmers want to change.
For instance
main()
{
float a;
a = (float)5 / 3;
}
gives result as 1.666666 . This is because the integer 5 is converted to floating point value before division and the operation between float and integer results in float.
Thus from the above it is clear that the usage of typecasting is to make a variable of one type, act like another type for one single operation. So by using this ability of typecasting it is possible for create ASCII characters by typecasting integer to its character equivalent.
Typecasting is also used in arithmetic operation to get correct result. This is very much needed in case of division where when integer gets divided the remainder is omitted. So to get correct precision value in this case one can make use of typecast ability as shown in example above. One more use of the typecasting is shown in example below
For instance if we have a code as below:
main()
{
int a = 5000, b = 7000 ;
long int c = a * b ;
}
Here two integers are multiplied and the result is truncated and stored in variable c of type long int. But this would not fetch correct result for all. So to get a more desired output in this case it would be desirable to have the code as
long int c = (long int) a * b;
Though it has so many uses one must take care about its usage since using it in wrong places might cause loss of data like for instance truncating a float when typecasting to an int.
How Typecasting plays an important role in C
C allows programmers to do typecasting by placing the type name in parentheses and placing this in front of the value programmers want to change.
For instance
main()
{
float a;
a = (float)5 / 3;
}
gives result as 1.666666 . This is because the integer 5 is converted to floating point value before division and the operation between float and integer results in float.
Thus from the above it is clear that the usage of typecasting is to make a variable of one type, act like another type for one single operation. So by using this ability of typecasting it is possible for create ASCII characters by typecasting integer to its character equivalent.
Typecasting is also used in arithmetic operation to get correct result. This is very much needed in case of division where when integer gets divided the remainder is omitted. So to get correct precision value in this case one can make use of typecast ability as shown in example above. One more use of the typecasting is shown in example below
For instance if we have a code as below:
main()
{
int a = 5000, b = 7000 ;
long int c = a * b ;
}
Here two integers are multiplied and the result is truncated and stored in variable c of type long int. But this would not fetch correct result for all. So to get a more desired output in this case it would be desirable to have the code as
long int c = (long int) a * b;
Though it has so many uses one must take care about its usage since using it in wrong places might cause loss of data like for instance truncating a float when typecasting to an int.
Profile Answers by GeekAdmin Questions by GeekAdmin
Questions by GeekAdmin answers by GeekAdmin
Related Answered Questions
Related Open Questions