Type Casting In C Language

0
150780
Type Casting in C Language
Type Casting in C Language

Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can typecast long to int. You can convert values from one type to another explicitly using the cast operator. There are two types of type casting in c language that are Implicit conversions and Explicit Conversions. In this article, we also learn about the difference between type casting and type conversions.

New data type should be mentioned before the variable name or value in brackets which to be typecast.

C type casting example program:

In the below C program, 7/5 alone will produce integer value as 1.
So, type cast is done before division to retain float value (1.4).

1 #include <stdio.h>
2 int main ()
3 {
4     float x;
5     x = (float) 7/5;
6     printf(“%f”,x);
7 }

Output:
1.400000

What is type casting in C Language?

Converting an expression of a given type into another type is known as type casting. typecasting is more use in c language programming.

Here, It is best practice to convert lower data type to higher data type to avoid data loss.

Data will be truncated when the higher data type is converted to lower. For example, if a float is converted to int, data which is present after the decimal point will be lost.

There are two types of type casting in c language.

Types of typecasting in C

S.No Types of type casting in C Programming
1 Implicit Conversion
2 Explicit Conversion

 

1. Implicit conversion

Implicit conversions do not require any operator for converted. They are automatically performed when a value is copied to a compatible type in the program.

Here, the value of a has been promoted from int to double and we have not had to specify any type-casting operator. This is known as a standard conversion.

Example :-

1 #include<stdio.h>
 2 #include<conio.h>
 3 void main()
 4 {
 5       int i=20;
 6       double p;
 7       clrscr();
 8  
 9       p=i; // implicit conversion
10  
11       printf(“implicit value is %d”,p);
12  
13       getch();
14 }

 

Output:-
implicit value is 20.

 

2. Explicit conversion

In C language, Many conversions, especially those that imply a different interpretation of the value, require an explicit conversion. We have already seen two notations for explicit type conversion.

They are not automatically performed when a value is copied to a compatible type in the program.

Example :-

 1 #include<stdio.h>
 2 #include<conio.h>
 3 void main()
 4 {
 5       int i=20;
 6       short p;
 7       clrscr();
 8  
 9       p = (short) i; // Explicit conversion
10  
11       printf(“Explicit value is %d”,p);
12  
13       getch();
14 }

Output:-
Explicit value is 20.

 

Usual Arithmetic Conversion

The usual arithmetic conversions are implicitly performed to cast their values in a common type, C uses the rule that in all expressions except assignments, any implicit type conversions made from a lower size type to a higher size type as shown below:

Type Casting In C Language
Type Casting In C Language

 

Inbuilt Typecast Functions In C:

There are many inbuilt type casting functions available in C language which performs data type conversion from one type to another.

 

S.No Typecast Function Description
1 atof()      Convert string to Float
2 atoi() Convert string to int
3 atol() Convert string to long
4 itoa() Convert int to string
5 ltoa() Convert long to string

We are recently taking a survey from different programmers who are available in google plus social media. We did a survey for type casting in c language.

Difference between type casting and type conversion

Whenever there is a need to convert one data type to another the two terms comes in our mind “typecasting” and “type conversion”. When the two types are compatible with each other, then the conversion of one type to other is done automatically by the compiler that is cold type conversion but remembers we can store a large data type into the other for example we can not store a float into int because a float is greater than int. whereas, typecasting is to be explicitly done by the programmer.

BASIS FOR COMPARISON TYPE CASTING TYPE CONVERSION
Definition When a user can convert the one data type into other then it is called as the typecasting. Type Conversion is that which automatically converts the one data type into another.
Implemented Implemented on two ‘incompatible’ data types. Implemented only when two data types are ‘compatible’.
Operator For casting a data type to another, a casting operator ‘()’ is required. No operator required.
Implemented It is done during program designing. It is done explicitly while compiling.
Conversion type Narrowing conversion. Widening conversion.
Example int x;
byte y;


y= (byte) x;
int x=3;
float y;
y=a; // value in y=3.000.

 

 You can download the pdf file for type casting in c language- pdf.

 

As we have discussed how we can easily type cast from float to int and int to char. Hope you cleared your learning concepts for c programming language. Happy coding! Please feel free to send your queries to [email protected]; we are happy to help you. 🙂

 

See More: Type Casting In Python