C Programming Tutorial
Type casting allows us to convert one data type into other. In C language, we use cast operator for type casting which is denoted by (type).
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.
New data type should be mentioned before the variable name or value in brackets which to be typecast.
Syntax:
(type)value;
C type casting example program:
In the below C program, 7/5 alone will produce integer value as 1.
#include <stdio.h>
int main ()
{
float x;
x = (float) 7/5;
printf(“%f”,x);
}
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 typecasting in c language.
TYPES OF TYPECASTING IN C
S.No | Types of typecasting 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 :-
#include<stdio.h>
#include<conio.h>
void main()
{
int i=20;
double p;
clrscr();
p=i; // implicit conversion
printf(“implicit value is %d”,p);
getch();
}
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 :-
#include
#include
void main()
{
int i=20;
short p;
clrscr();
p = (short) i; // Explicit conversion
printf(“Explicit value is %d”,p);
getch();
}
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:
Inbuilt Typecast Functions In C:
There are many inbuilt typecasting 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 “type casting” 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 remember 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, type casting 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 type casting. | 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; |
|
|