Type Conversion in C
C – Type Conversion
Type conversion is a procedure by which a data type can be converted into any other possible data types.The task of type conversion in C is to be performed by the compiler at the time of compilation. In this process, the data type in which the existing data type wants to be converted should be higher than the existing data type.
There are two types of Type Conversions:
- Implicit Type Conversion
This type of conversion is done by the compiler on its own without any external command or code and thus,also known as automatic type conversion. All the data types are converted to the highest data type of any given expression . - Explicit Type Conversion
This type of conversion is to be done from the user end by writing code for the same and thus, also known as type casting. By using the syntax for type casting, a data type can be converted into any specified data type..
Note:
There is one major drawback of data loss in implicit type conversion as
some data could be lost forever while being converted to a higher data
type.
Syntax of Explicit conversion:
(data_type)expression;
Hierarchy of Data Types:
Example of Implicit Conversion:
Run
#include<stdio.h> int main() { int x = 10; char y = 'a'; x = x + y; float z = x + 1.0; printf("x = %d, z = %f", x, z); return 0; }
Output:
x = 107, z = 108.000000
Example of Explicit Conversion:
#include<stdio.h> int main() { double x = 1.2; int sum = (int)x + 4; printf("sum = %d", sum); return 0; }
Output:
sum = 5
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment