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.
Example:
#include<stdio.h>
int main(void)
{
int a=5,b=8;
float c = 0, d = 0;
c = a/b;
printf("n [%f] n",c);
d = (float)a/(float)b;
printf("n [%f] n",d);
return 0;
}
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]
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.
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
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;
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’*/
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*/
THE END

Typecasting in c

  • 1.
    Typecasting in C Typecastingis 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.
  • 2.
    Example: #include<stdio.h> int main(void) { int a=5,b=8; floatc = 0, d = 0; c = a/b; printf("n [%f] n",c); d = (float)a/(float)b; printf("n [%f] n",d); return 0; }
  • 3.
    In the aboveexample, 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: Resulttype 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 (automatictransformation) 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 (giventransformation) 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: floata = 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*/
  • 9.