Typecasting in C allows changing the data type of a variable, regardless of its original definition. When a variable is typecast to a new type, the compiler treats it as the new type. In the example, dividing two integers results in 0, but casting them to floats first results in the actual quotient of 0.625. Typecasting can be implicit, letting smaller types automatically cast to larger ones in expressions, or explicit using cast operators like (int) or (float). Explicit casts have higher priority and force a specific conversion.
C Typecasting Guide: Change Variable Data Types Using Casts
1. Typecasting in C
Typecasting is simply a mechanism
by which we can change the data type of a
variable, no matter how it was originally
defined. When a variable is typecasted into a
different type, the compiler basically treats the
variable as of the new data type.
3. In the above example, first we divide 'a' by 'b' without
typecasting. While in the second attempt we typecast both 'a'
and 'b' to float and then divide them.
So, what happens is, in the first attempt, the compiler
knows both 'a' and 'b' are integers so it ignores the floating
part of the result and stores the result of a/b in a temporary
integer variable and then assign the value of this temporary
integer variable to 'c'. So despite of 'c' being a float, the final
value that 'c' gets is an integer ie '0' in this case.
While in the second attempt, as both 'a' and 'b' are
individually typecasted as 'float', so this typecasting makes
compiler think as if both 'a' and 'b' are floats, so the compiler
retains the floating part of the result and assigns the float
value to 'c'.
The output of the above program :
[0.000000]
[0.625000]
4. CASTING DATA TYPES:
Result type of arithmetic phrase depends of
operand types inside it. When a phrase is consisted of
various data types, result type is set by defined casting
rules. Rules for casting various data types in C
language are oriented to higher data type. There are
two ways of casting data types in C:
Implicit(automatic) casting
2. Explicit(given) casting
1.
5. Implicit Casting (automatic transformation)
works in a way that a variable (operand) of data
type that is smaller in length (than data type of
second variable) (operand), transforming
internally to variable of data type with longer
number length. It may sound mixed up.
Example:
int a;
float f, g;
g = a + f; // a transforms to float
6. Explicit Casting (given transformation) of data types
has higher priority then automatic transformation.
General declaration of explicit (given) cast (cast
operator):
(data_type) operand
Operand can be variable or phrase.
Example:
a = (int) c;
b = (double)d + c;
7. Some examples:
Example 1:
float a = 5.25;
int b = (int)a;
/*Explicit casting from float to
int. The value of b here is 5*/
Example 2:
char c = ’A’;
int x = (int)c;
/*Explicit casting from char to
int. The value of x here is 65: the ASCII code of
‘A’*/
8. Example 3:
int x=7, y=5 ;
float z;
z=x/y;
/*Here the value of z is 1*/
If we want to get the exact value of 7/5 then
we need explicit casting from int to float:
int x=7, y=5;
float z;
z = (float)x/(float)y; /*Here the value of z is
1.4*/
Example 4:
int x=70;
char c =(char)x; /*The value of c here is F:
the ASCII code of 70*/