C Programming

Typecasting in C

Typecasting is the process of converting an attribute from one type of data to a new one. While it creates perfect sense, the compiler can transform one kind of data into something else. In case we add an integer data type value to a floating-point mutable keyword, the compiler converts it to a “float” value. Casting permits us to create this type of transformation transparent or compel this when it would not otherwise occur. Two types of Casting that have been used in the C language. We will discuss both of the typecasting kinds in our guide.

  • Implicit Type Casting
  • Explicit Type Casting

Syntax

(Type)value
Type shows the data type to which the variable has been transformed. While the “value” is a variable to be translated to another type. At the start, we have to log in from the Linux operating system. In our case, we have been using Ubuntu 20.04 Linux system at the time of writing this guide. You have to just install some C language compiler on your system to compile the code. We have been using the “GCC” compiler in Ubuntu 20.04 Linux operating system. Try using the stated command in the shell to install it:

$ sudo apt install gcc

Implicit Type Casting

Implicit type conversion is the process of transforming data types despite losing their significance. Whenever you want to switch data types deprived of altering the meaning of the information contained within the element, this form of typecasting is needed. When a variable is replicated to a compliant data format, implicit type translation occurs instantly. Let’s have a simple illustration of implicit type casting in our Linux system to demonstrate the working of typecasting. So open the command line terminal in the Linux system after logging in. Use “Ctrl+Alt+T” for a quick opening. The GNU editor has been used to write C code so create a quick C language file “one.c” in it while using the “nano” query.

$ nano one.c

As the GNU editor has been opened properly, you have to write the below-presented code in it. Let’s discuss this code.

So, it has some libraries needed to list in the code first. After that, we have created the main function and declared short and integer type variables “a” and “b” in it. Then we have used implicit typecasting as “b=a”. This is assigning the “short” value of variable “a” to an integer type variable “b”. Now both the variables have the same values without changing the value of the first variable. This way, we have successfully translated the data type of variable “b”. After that, both the variable values have been printed out and the main method will be ended.

Press “Ctrl+S” to save the code. Now quit the file via the “Ctl+X” shortcut. Use the “GCC” compiler to compile the “one.c” C language file. No compilation error occurs, this indicates the correction of a code.

$ gcc one.c

Execution of this file has been done using the “a.out” query in the shell and the output is showing the same values for both the variables and print statements separately.

$ ./a.out

Explicit Type Casting

Explicit casting is achieved by directly putting the type in brackets before the value. We had a look at the implicit data type that is spontaneously transformed into implicit type transformation. In certain cases, we ought to force type conversion. In such situations, explicit type casting is required. We have to use a typecasting operator while using this type. So let’s have an example of explicit type casting in our guide.

So in the Ubuntu 20.04 Linux system, we have created a file “one.c” having “C” extension at the end of it to make it a “c” language file. This file has been generated via the same “nano” command and to automatically open it in the GNU Nano Editor.

$ nano one.c

The file has been opened in the GNU editor. You can see the below code. You have to type this code in your nano file.

We have some libraries included first. Then we have declared a main function with the “int” return type. In this main method, we have declared two integer type variables “x” and “y” with values “7” and “9”, respectively. We have also declared a variable “z” having a “float” data type. You have to understand that you cannot assign the other than “float” data type value to a variable “z”. This means that we have to convert some values first, then we can add them to it. So, we have to use explicit type casting here to convert both the integer variable “x” and “y” into float “x” and “y”, respectively. This has been done using the “float” keyword in the brackets shown below. Then we have divided both values while using “x” as the numerator, and “y” as the denominator. The resultant value has been stored in the float type variable “z”. After that, we have cast-off the print statement to show the resultant value of “z” on the terminal screen and the method will be ended. Save the file via the “Ctrl+S” key. Close the file “one.c” using the “Ctrl+X” shortcut key.

Now, you have to compile the code first. For that, try out the stated-below “gcc” query in the shell command terminal with the name of a file “one.c”. As the compilation command doesn’t show any error, this means our code is correct.

$ gcc one.c

After the successful compilation of code, we executed the file using the “a.out” execution query, and found the below float resultant value for the variable “z”.

$ ./a.out

Conclusion

In this guide, we have discussed the two different types of casting techniques to translate one data type variable into another, e.g., Implicit and Explicit. While using typecasting in your programming, don’t get confused between both the typecasting types.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.