Type Casting in C

Typecasting allows us to convert one data type into other. In C language, we use cast operator for typecasting which is denoted by (type).

Syntax:

snippet
(type)value;
Note
Note: It is always recommended to convert the lower value to higher for avoiding data loss.

Without Type Casting:

snippet
int f= 9/4;
printf("f : %d\n", f );//Output: 2

With Type Casting:

snippet
float f=(float) 9/4;
printf("f : %f\n", f );//Output: 2.250000

Type Casting example

Let's see a simple example to cast int value into the float.

snippet
#include
int main(){
float f= (float)9/4;  
printf("f : %f\n", f );  
return 0;
}

Output:

Output
f : 2.250000
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +