Understanding of Type Casting and Type Conversion in C Language

Type casting, also known as type conversion, is the process of converting one data type into another. In C program

ming, this is a crucial concept that allows you to manipulate and work with different data types effectively. Whether you need to store a ‘long’ value as a simple integer or perform operations involving different data types, type casting plays a vital role.

To perform type casting explicitly in C, you can use the cast operator, which has the following syntax:

(type_name) expression

Let’s explore an example to illustrate how the cast operator works. In this example, we perform division between two integer variables, but we want the result to be a floating-point value:

#include <stdio.h>

int main() {
   int sum = 17, count = 5;
   double mean;

   mean = (double) sum / count;
   printf("Value of mean: %f\n", mean);
   return 0;
}

When you compile and run this code, it produces the following result:

Value of mean: 3.400000

It’s important to note that the cast operator takes precedence over division. In the example, the ‘sum’ variable is first converted to type ‘double,’ and then the division operation is carried out, resulting in a ‘double’ value.

Type conversions in C can be either implicit or explicit. Implicit type conversions are automatically performed by the compiler when necessary, while explicit conversions use the cast operator. It is considered good programming practice to use the cast operator whenever type conversions are needed to ensure clarity and precision in your code.

Integer Promotion

Another aspect of type conversion in C is integer promotion, which involves converting values of integer types smaller than ‘int’ or ‘unsigned int’ to either ‘int’ or ‘unsigned int.’ Let’s see an example where we add a character to an integer:

#include <stdio.h>

int main() {
   int i = 17;
   char c = 'c'; /* ASCII value is 99 */
   int sum;

   sum = i + c;
   printf("Value of sum: %d\n", sum);
   return 0;
}

When you compile and run this code, it produces the following result:

Value of sum: 116

In this case, the ‘sum’ value is 116 because the compiler performs integer promotion by converting the value of ‘c’ to its ASCII representation before adding it to ‘i.’

Usual Arithmetic Conversion

The usual arithmetic conversions in C are implicitly carried out to cast values to a common type. These conversions follow a hierarchy, and integer promotion is the first step. If the operands still have different types after promotion, they are converted to the type that appears highest in the hierarchy. However, it’s important to note that usual arithmetic conversions are not performed for assignment operators or logical operators like ‘&&’ and ‘||.’

Let’s examine an example to understand this concept:

#include <stdio.h>

int main() {
   int i = 17;
   char c = 'c'; /* ASCII value is 99 */
   float sum;

   sum = i + c;
   printf("Value of sum: %f\n", sum);
   return 0;
}

When you compile and run this code, it produces the following result:

Value of sum: 116.000000

In this example, ‘c’ is first converted to an integer, and then usual arithmetic conversion applies, leading the compiler to convert ‘i’ and ‘c’ into ‘float’ before adding them, resulting in a ‘float’ result.


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading