C type casting

In c, typecasting is to convert a variable data type, into another data type.

Example

Convert an int value to float value.

#include<stdio.h>
#include<conio.h>

void main()
{
	int value = 25;
	float amount = (float) value;
	printf("%f", amount);
}
Output
25.00

Types of type casting in c

  1. Implicit Conversion
  2. Explicit Conversion

Implicit Conversion

This Conversion is automatically done by compiler, from a smaller data type to biggest data type. No data loss in this conversion.

Example

#include<stdio.h>
#include<conio.h>

void main()
{
	int value = 35;
	printf("%f", amount);
}
Output
35.00

Explicit Conversion

This Explicit Conversion is done by programmer, In this conversion a variable with bigger data type is converted into smaller data type. Possibilities of data loss is there.

For Explicit conversion, type casting operator parenthesis () is used. inside this parenthesis, data type is used.

Syntax

(data_type) variable

Example

#include<stdio.h>
#include<conio.h>

void main()
{
   int a = 250
   float v = (float) a;
   printf("%f", v);

   float no1 = 2.5;
   int no2 = 5;
   double d = (double) 2.5/5;
}