We have seen that compiler causes internal type conversion automatically when it has to handle operands of different type.
There is also provision for programmer to explicitly convert value of an expression into a different data type. To do so, expression must be preceded by name of desire data type enclosed in parenthesis.
This is called type casting done with a unary operator cast.
The expression x/y will cause integer division (i.e. decimal part will be truncated) giving result 1. It will then be converted to float (1.00) since it is to be assigned to “a” which is a float.
But if we want that the result should not be truncated and some other factors don’t allow us to define x as float then we can use type casting.
The float(x), converts x into a floating point value before performing the division so that result is floating point. The conversion produces x into proper type. The value of x is not changed permanently as seen from the printf statement that follows.
Another example can be given as a function ‘sqrt’ which takes double as argument and would give nonsense results if some other type of data is given.
x is converted to type double.
C Related Interview Questions
|DBMS Interview Questions||C++ Interview Questions|
|C & Data Structures Interview Questions||Java Interview Questions|
|Go (programming language) Interview Questions||F Sharp (programming language) Interview Questions|
|C preprocessor Interview Questions||R Programming language Interview Questions|
|D Programming Language Interview Questions||Lua (programming language) Interview Questions|
|Embedded C Interview Questions|
Introduction To C-programming
Data Types And Storage Classes
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.