0% found this document useful (0 votes)
9 views2 pages

8-Type Conversion in C

The document explains type conversion in C, which involves converting one data type to another either automatically (implicit) or manually (explicit). Implicit type conversion occurs when the compiler automatically converts data types, while explicit conversion requires the programmer to use a casting operator. Examples illustrate both types of conversion, highlighting potential information loss during the process.

Uploaded by

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

8-Type Conversion in C

The document explains type conversion in C, which involves converting one data type to another either automatically (implicit) or manually (explicit). Implicit type conversion occurs when the compiler automatically converts data types, while explicit conversion requires the programmer to use a casting operator. Examples illustrate both types of conversion, highlighting potential information loss during the process.

Uploaded by

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

Type Conversion in C

Last Updated : 22 Jan, 2025

In C, type conversion refers to the process of converting one data type to another. It can be done automatically by the compiler or
manually by the programmer. The type conversion is only performed to those data types where conversion is possible.

Let’s take a look at an example:

#include <stdio.h>

int main() {
float f1 = 5.5;
int a;

// Automatic type conversion from float to int


a = f1;

// Manual type conversion from int to float


float f2 = (float)a;

printf("a: %d\n", a);


printf("f1: %f\n", f1);
printf("f2: %f", f2);
return 0;
}

Output

a: 5
f1: 5.500000
f2: 5.000000

Explanation: In the above program, when f1 is assigned to int a, its value is automatically converted to integer first and then assigned
to a. On the other hand, we manually convert the value of a to assign it to float variable f2.

The automatic type conversion performed by the compiler is called Implicit Type Conversion and the manual one done by the
programmer is called Explicit Type Conversion.

Implicit Type Conversion


Implicit type conversion, also known as type coercion, occurs when the C compiler automatically converts one data type to another
without the need for explicit instructions from the programmer. This typically happens when a smaller data type is assigned to a larger
data type or when different data types are involved in an arithmetic operation.

For example, if an integer is added to a float, the integer is implicitly converted to a float, and the result is a float.

Example

#include <stdio.h>

int main() {
int n1 = 5;
float n2 = 4.5;

// Implicit type conversion from int to float


float result = n1 + n2;

printf("%.2f\n", result);
return 0;
}

Output
9.50

Explanation: In this code, implicit type conversion occurs when n1 (int) is automatically converted to a float during the expression n1 +
n2 to match the type of n2.

Note: It is possible for type conversions to lose information. Signs can be lost when signed is implicitly converted to unsigned, and
overflow can occur when long is converted to float.

When Implicit Type Conversion Occurs?

Some of its few occurrences are mentioned below:

1. Conversion Rank:
Refers to the priority assigned to data types during conversions, where types with higher rank (e.g., double) are converted to lower
rank types (e.g., int) only when necessary.
2. Conversions in Assignment Expressions:
Occurs when a value is assigned to a variable of a different type, e.g., assigning a float to an int results in truncation of the decimal
part.
3. Conversion in Other Binary Expressions:
Happens when operators are used on operands of different types. The compiler converts them to a common type for compatibility (e.g.,
adding an int to a float).
4. Promotion:
Involves converting a smaller data type to a larger one (e.g., int to float).
5. Demotion:
Converts a larger data type to a smaller one (e.g., float to int), often leading to loss of precision or data truncation.

Explicit Type Conversion


Explicit type conversion, or typecasting, occurs when the programmer explicitly tells the compiler to convert a variable from one type to
another. This is done using the casting operator (type).

Syntax

(type) expression

where type indicates the final data type to which the expression is converted.

Example

#include <stdio.h>

int main() {
float n1 = 7.9;
int n2;

// Explicit type conversion (casting)


// from float to int
n2 = (int)n1;

printf("%d", n2);
return 0;
}

Output

You might also like