Type Conversion in the C
The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that can be converted. A compiler is responsible for all the type conversions in the C.
The final data type cannot be less than the original data type after the type conversion. Type conversion, also known as "widening conversion," occurs only during the compilation period.
Type conversion comes in two varieties. They are:
1. Explicit Type Conversion
2. Implicit Type Conversion
1. Explicit Type Conversion: We manually transform values from one data type to another in the explicit type conversion. Explicit Type Conversion is also known as the type casting method, and it is also user-defined. Users can typecast the output here to generate it of a specific data type.
In the C programming language, the syntax for explicit type conversion is
(data type) expression
Example
#include<stdio.h>
int main()
{
/* Here create a float variable */
float num_1 = 96.125626;
printf("The given decimal value is: %.6f\n", num_1);
/* Explicit data type conversion is performed here */
double num_2 = (double) num_1;
printf("The value after type conversion is: %.2lf", num_2);
return 0;
}
Output:

In the preceding program, we declared a float variable named "num_1" with the value 96.125626. Take note of the following code snippet:
/* Explicit data type conversion is performed here */
double num_2 = (double) num_1;
Here,
(double) indicates the data type that "num_1" should be converted to.
The value num_1 will be converted to double data type.
Example
#include<stdio.h>
int main()
{
/* Here create a float variable */
int num_1 = 106;
printf("The given integer value is:%d\n", num_1);
/* (char) is a function that converts a number to a character. */
char alpha_1 = (char) num_1;
printf("The value after type conversion is: %c", alpha_1);
return 0;
}
Output:

In the preceding program, we declared an int variable named "num_1" with the value 106. Take note of the following code snippet:
/* (char) is a function that converts a number to a character. */
char alpha_1 = (char) num_1;
Here,
(char) converts "num_1" (an integer data type) to a character data type specifically.
The value num_1 will be converted to char data type.
2. Implicit Type Conversion: In implicit type conversion, the value of one data type is automatically transformed to another data type. Automatic type conversion is another name for implicit type conversion.
This is entirely handled by the compiler, with no external help. This usually happens when more than one data type is contained in an expression. In such cases, type conversion occurs to prevent any data loss.
All variable data types are updated to the data type of their variables with the greatest data type.
bool data type < char data type < short int data type < int data type < unsigned int data type < long data type < unsigned data type < long long data type < float data type < double data type < long double data type
Implicit conversions may lose information, signs may be lost (when a signed value is implicitly converted to an unsigned value), and overflow is possible. (The long data type is implicitly converted to a float data type.)
Implicit Type Conversion Occurs in the C
Some of its occurrences are listed below:
1. "Rank of Conversion" is an example of implicit type conversion.
2. “Assignment expression conversions”
3. Implicit data type conversion can also be seen in promotions and demotions.
Example
#include<stdio.h>
int main()
{
/* Here create a float variable */
double num_1 = 5185.89;
printf("The given double value is: %.2lf\n", num_1);
/* Converting a double value num_1 to an integer */
int num_2 = num_1;
printf("The value after type conversion is: %d", num_2);
return 0;
}
Output:

In the preceding program, we declared a double variable named "num_1" with the value 5185.89. Take note of the following code snippet:
/* Converting a double value num_1 to an integer */
int num_2 = num_1;
The C language compiler automatically transforms the double number 5185.89 to the integer value 5185 in this case.
Because the conversion occurs automatically, this form of conversion is known as implicit type conversion.
Example
#include<stdio.h>
int main()
{
/* Here create a float variable */
char alpha_1 = 'k';
printf("The given char value is:%c\n", alpha_1);
/* Converting a char value alpha_1 to an integer */
int num_1 = alpha_1;
printf("The value after type conversion is: %d", num_1);
return 0;
}
Output:

In the preceding program, we declared a char variable named "alpha_1" with the value ‘k’. Take note of the following code snippet:
/* Converting a char value alpha_1 to an integer */
int num_1 = alpha_1;
The C language compiler automatically transforms the char value ‘k’ to the integer value 107 in this case.