Type Casting in C Language – LinuxWays
One of the first things that we must learn when dealing with programming in C is that this language deals with different types of data, and that each of them is related to the size it occupies in memory, the bit precision of the system, and the way it is organized in the storage area. The C language is strict in dealing with data types because that is the first thing that is specified when a variable is declared.
When we develop the code in the C language, there are cases when we need to convert the value of a variable from one data type to another like, for example, when we need to use the value that is stored in it as the input or output argument of a function that accepts a different data type.
In some cases, the compiler performs the implicit type conversions that are performed automatically. In other cases, we must perform a casting operation on the variable to convert its type.
In this Linux Ways article, you’ll learn what data type castings are in variables, arrays, and pointers. We will also explain in which cases the compiler does the casting automatically and when you have to specify it by an explicit operation. In each section, you’ll find practical examples with code fragments and images that show you everything you need to know about type conversions in the C language.
Implicit Casting in the C Language
In the C language, arithmetic or assignment operations are always performed between operands of the same data type. If this is not the case, the compiler performs a conversion on the smaller variable to convert its value to the same data type of the other operand.
This is called an implicit cast and it happens automatically when we perform the operations between variables with different data types. In casts, the data type of the variable is not changed, but the compiler converts the size and format of the value to perform the operation. We can see this in the following example:
void main()
{
int a =55;
char b= 23;
int c;
c = a + b;
printf (“a + b = %in“, c);
}
In this case, an implicit cast of type int was performed in the variable “b” of type char to be able to operate with “a”. Let’s see an image with the result of this operation with implicit casting:
Let’s look at the following example in which the variable “a” of type int and the variable “b” of type float perform a division operation of 3 / 2 and store their result in the integer “c”:
void main()
{
int a =3;
float b= 23;
float c;
c = a / b;
printf (“a / b = %fn“, c);
}
In this case, the variable of type int is cast to type float. Let’s see an image with the execution of this code:
As we can see in the figure, by implicitly casting in the variable “a”, we were able to perform an operation that gave an exact result of division by treating both variables as floating point numbers.
Explicit Casting in the C Language
As we have seen so far, the compiler automatically performs implicit castings when the operands are of different types. However, there are cases where, for example, we need to perform an operation on two variables of the same type, but treat them as variables of a different type. Let’s look at this with an example:
void main()
{
int a =3;
int b= 2;
float c;
c = a / b;
printf (“a / b = %fn“, c);
}
In the given code, a division is performed between the variables “a” and “b” of type int, giving the result of 1 and leaving a remainder of 1.
In these cases, we can treat the variables “a” and “b” as data of type Float and thus obtain an exact result in the variable “c”. To do this, we must explicitly tell the compiler to perform typecasting on them. Let’s see the syntax for explicit casting operations:
(Type) variable or expresion
To do the explicit casting, we need to specify the type that we want to cast the variable to in parentheses and then the variable or expression that we want to cast. Let’s see the previous code where we performed a casting to the type float in variables “a” and “b”.
void main()
void main()
{
int a =3;
int b= 2;
float c;
c = (float) a / (float) b;
printf (“a / b = %fn“, c);
}
In this case, the variables “a” and “b” are treated as floating point numbers which leads to an exact result when dividing.
In this case, the variables “a” and “b” were cast separately. But as we saw earlier in the syntax, we can also cast the expression, which, as we can see in the following, simplifies the code:
void main()
{
int a =3;
int b= 2;
float c;
c = (float) a / b;
printf (“a / b = %fn“, c);
}
Explicit casting can happen anywhere in the code, be it in an operation, within the arguments that we send to a function, etc.
Pointers Casting in the C Language
In C language, pointers also have certain data types. This means that for certain assignment operations, or when we use pointers as input or output arguments to functions, errors may occur at compile time due to type incompatibility. The following code declares a pointer of type char which is then used as the output argument of the getchar() function:
The getchar() function recovers a character from the input but returns it in the int data type, which is why the compiler issues the following error:
In this case, we need to do an explicit casting in the c-pointer. This is done by placing the type of the pointer in front of the “*” symbol in parentheses as seen in the following code:
As seen in the following figure, casting the pointer fixes the compilation error that occurred previously:
Conclusion
In this Linux Ways article, we just saw the types of casting that can be done in the C language. We showed you how to distinguish the cases where the compiler performs casting implicitly and automatically, and when you need to specify the type conversion explicitly. Since programming in this language involves a lot of work with pointers, we also included a section on converting this type.
I am the Founder of Clipping Path Creative Ltd (Outsource Photo Editing Company) based in the UK. Also, I am a professional WordPress expert and working on Fiverr since 2016. I am offering all kinds of WordPress issues fix, Content upload and formatting, providing & installing SSL, Website migration, and website creation at affordable prices. Already achieved 5000+ reviews on Fiverr.