Type Conversion in C

What is Type Conversion in C

Type conversion in c is a way to convert one data type into another one. It is also known as data conversion or type casting. The conversion is done only between those datatypes wherein the conversion is possible ex – char to int and vice versa.

         C Language Provides two methods of type casting :

    1. Implicit type conversion

    2. Explicit type conversion

Type Casting in C

 

1. Implicit type conversion

Implicit type casting means the conversion of data types from one to another without losing their original meaning or sense. This type of conversion is usually performed by the compiler when necessary without any commands by the user. Thus it is  Automatic Type Conversion”.This type casting program automatically converts the variable from one data type to another. 

An implicit type conversion data type of variables as it always converts lower data types to higher ones. And program automatically converts the variable from one data type to another.

 

program

#include<stdio.h> 
int main() 
{ 
    short a=20;            //initializing variable of short data type 
    int b;                 //declaring int variable 'b'. 
    b=a;                  //Implicit type casting 
    printf("%d\n",a); 
    printf("%d\n",b); 
}

 

Output

20
20

 

 

2. Explicit type conversion

The type conversion performed by the programmer by posing the data type of the expression of a specific type is explicit type conversion. It is user-defined conversion.

 

Program

#include<stdio.h> 
int main() 
{ 
    int a; 
    float b; 
    char c; 
    printf("Enter the value of a\n"); 
    scanf("%d",&a); 
    printf("A is %d\n", a); 
    printf("Enter the value of b\n"); 
    scanf("%f",&b ); 
    printf("B is %d\n", (int) b); 
    printf("Type any character : \n"); 
    scanf(" %c",&c ); 
    printf("Character is %d",(int) c); 
    return 0; 
}

 

Output

Enter the value of a

1

A is 1

Enter the value of b

2

B is 2

Type any character :

a

Character is 97

 

Also read Symbolic Constant in C

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *