Type Casting in C with examples
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:
(type)value;
Without Type Casting:
int f= 9/4;
printf(“f : %d\n”, f );//Output: 2
With Type Casting:
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.
#include<stdio.h>
int main(){
float f= (float)9/4;
printf(“f : %f\n”, f );
return 0;
}
Output:
f : 2.250000
Related Post:
Features of C Programming Language
Dynamic memory allocation in C
Difference Between Type Casting and Type Conversion in C
Difference Between Variables and Constants
C Program to generate Fibonacci Triangle
C Program to print “hello” without semicolon
C Program to swap two numbers without third variable
Count the number of digits in C