This is our next tutorial. Today we are going to see Typecasting in C programming language. Let’s begin.
You can also read pointers in c, embedded interview topics, and bitwise operators in c.
Introduction
Typecasting 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 type cast long to int. You can convert values from one type to another explicitly using the cast operator.
The new data type should be mentioned before the variable name or value in brackets which to be typecast.
Typecasting example program
In the below C program, 7/5 alone will produce an integer value of 1. So, typecast is done before division to retain float value (1.4).
#include <stdio.h> int main () { float x; x = (float) 7/5; printf(“value = %f”,x); }
Output:
value = 1.400000
What is type casting in C Language?
Converting an expression of a given type into another type is known as typecasting. 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 float is converted to int, data which is present after decimal point will be lost.
There are two types of type casting in c language.
Types of typecasting in C
- Implicit Conversion
- Explicit Conversion
Implicit conversion
Implicit conversions do not require any operator for conversion. 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 typecasting operator. This is known as a standard conversion.
Example:-
#include<stdio.h> #include<conio.h> void main() { int i=20; double p; p=i; // implicit conversion printf(“implicit value is %d”,p); }
Output:-
implicit value is 20
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<stdio.h> #include<conio.h> void main() { int i=20; short p; p = (short) i; // Explicit conversion printf(“Explicit value is %d”,p); }
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 |
You can also read the below tutorials.
Embedded Software | Firmware | Linux Devic Deriver | RTOS
Hi, I’m SLR. I am a tech blogger and an Embedded Engineer. I am always eager to learn and explore tech-related stuff! also, I wanted to deliver you the same as much in a more straightforward way with more informative content. I generally appreciate learning by doing, rather than only learning. If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning!