Typecasting


Typecasting in C

Typecasting is converting a variable from one data type to another. If it makes sense, the compiler will convert one kind of data into another. When you give an integer value to a floating-point variable, the compiler converts it to afloat.

Typecasting or type-conversion is the process of converting one data type to another. If you wish to convert a 'long' value to a basic integer, type cast 'long' to 'int'.
To explicitly convert values from one type to another, use the cast operator, as seen below:

(type_name) expression

Consider the following example, in which the cast operator converts the division of two integer variables into a floating-point operation:

#include <stdio.h> main() { count = 5; int sum = 17; double mean; mean = (double) sum / count; printf("Value of mean : %f\n", mean ); }

When the preceding code is built and run, the following result is obtained:
3.400000 is the mean value.
It's worth noting that the cast operator takes precedence over division in this case. Thus, the value of sum is first changed to type double, then divided by count, producing a double value.
Type conversions can be implicit, in which case the compiler does the conversion automatically or explicit, the cast operator is used.
When type conversions are required, it is considered good programming practice to utilize the cast operator.

Integer Promotion

The process of converting values of integer types "smaller" than int or unsigned int to int or unsigned int is known as integer promotion.

Consider the following example of combining a character with an integer:

#include <stdio.h> main() { int i = 17; char c ='c'; /* the ascii value is 99 */ int sum; sum = i + c; printf("Value of sum : %d\n", sum ); }

When the preceding code is built and run, the following result is obtained: Value of sum: 116

Because the compiler conducts integer promotion and transforms the value of 'c' to ASCII before doing the actual addition operation, the result of the total is 116.

Usual Arithmetic Conversion

The standard arithmetic conversions are done implicitly to cast their values to a common type. If the operands still have various types, the compiler performs integer promotion first, then converts them to the type that occurs highest in the following hierarchy.

The assignment operators and the logical operators && and || are not subjected to the standard arithmetic conversions.
To further grasp the notion, consider the following example.
The assignment operators and the logical operators && and || are not subjected to the standard arithmetic conversions.

#include <stdio.h> main() { int i = 17; char c = 'c'; /* ascii value is 99 */ float sum; sum = i + c; printf("Summary value: percent fn", sum ); }

When the preceding code is built and run, the following result is obtained:

116.000000 is the total value of the amount.

It's easy to see that the initial c is transformed to an integer, but the final value is double.

implicit type casting:

#include<stdio.h> #include<conio.h> void main() { int a; float b=5.50; a=b; printf("%d",a); }

output:

5

explicit type casting:

#include<stdio.h> #include<conio.h> void main() { int a=5,b=2; float c; c=(float)a/b; printf("%f",c); getch(); }

output:

2.500000