Explicit type conversion in C | Casting in C

Preview

Explicit type conversion in C | Casting in C

It is the way to convert the output of an arithmetic expression into a specific data type. The syntax of casting is:

(Data_Type) Arithmetic_Expression

Data_Type is any data type of C in which we want to convert the output of an expression.

Arithmetic_Expression is any arithmetic expression of C which we want to convert into a particular data type.



Program for explicit conversion.
#include<stdio.h>
int main()
{
int a,b;
float c;
a=11;
b=4;
c=(float)a/b;         /*Line 8*/
printf(“\nc=%f”,c);
return(0);
}
Output
c=2.75
Description
In line 8  a and b int type variables. so result of a/b should be of int type i.e. 2 but due to casting by float result would become 2.75.

Best Books of C





Lesson tags: casting in c, data type conversion in c, explicit type conversion in c, user defined data conversionin c
Back to: C Programming Language