0% found this document useful (0 votes)
11 views4 pages

Type Conversion in C

The document explains type conversion in C, detailing two types: implicit and explicit. Implicit conversion occurs automatically by the compiler to prevent data loss, while explicit conversion, or type casting, is user-defined. It also highlights the advantages of type conversion for handling expressions with different data types.

Uploaded by

kothitarun130301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Type Conversion in C

The document explains type conversion in C, detailing two types: implicit and explicit. Implicit conversion occurs automatically by the compiler to prevent data loss, while explicit conversion, or type casting, is user-defined. It also highlights the advantages of type conversion for handling expressions with different data types.

Uploaded by

kothitarun130301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Type Conversion in C

 Difficulty Level : Easy


 Last Updated : 25 Nov, 2020
A type cast is basically a conversion from one type to another. There
are two types of type conversion:
1. Implicit Type Conversion

Also known as ‘automatic type conversion’.


 Done by the compiler on its own, without any external
trigger from the user.
 Generally takes place when in an expression more
than one data type is present. In such condition type
conversion (type promotion) takes place to avoid loss
of data.
 All the data types of the variables are upgraded to the
data type of the variable with largest data type.

 bool -> char -> short int -> int ->
 unsigned int -> long -> unsigned ->
 long long -> float -> double -> long
double
 It is possible for implicit conversions to lose
information, signs can be lost (when signed is
implicitly converted to unsigned), and overflow can
occur (when long long is implicitly converted to float).
Example of Type Implicit Conversion:

// An example of implicit conversion

#include<stdio.h>

intmain()

intx = 10; // integer x

chary = 'a'; // character c

// y implicitly converted to int. ASCII

// value of 'a' is 97

x = x + y;

// x is implicitly converted to float

floatz = x + 1.0;

printf("x = %d, z = %f", x, z);

return0;

}
Output:
1. x = 107, z = 108.000000
2. Explicit Type Conversion–

This process is also called type casting and it is user defined. Here the
user can type cast the result to make it of a particular data type.
The syntax in C:
(type) expression
Type indicated the data type to which the final result is converted.

// C program to demonstrate explicit type casting

#include<stdio.h>

intmain()

{
doublex = 1.2;

// Explicit conversion from double to int

intsum = (int)x + 1;

printf("sum = %d", sum);

return0;

Output:
sum = 2
Advantages of Type Conversion
 This is done to take advantage of certain features of type
hierarchies or type representations.
 It helps us to compute expressions containing variables of
different data types.

You might also like