• C Programming Video Tutorials

Type Casting in C



The term "type casting" refers to converting one datatype into another. It is also known as "type conversion". There are certain times when the compiler does the conversion on its own (implicit type conversion), so that the data types are compatible with each other.

On other occasions, the C compiler forcefully performs the typecasting (explicit type conversion), which is caused by the typecasting operator. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'.

You can use the typecasting operator to explicitly convert the values from one type to another −

(type_name) expression

Example 1

Consider the following example −

#include <stdio.h>

int main() {

   int sum = 17, count = 5;
   double mean;

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

}

Output

Run the code and check its output −

Value of mean: 3.000000

While we expect the result to be 17/5, that is 3.4, it shows 3.000000, because both the operands in the division expression are of int type.

Example 2

In C, the result of a division operation is always in the data type with a larger byte length. Hence, we have to typecast one of the integer operands to float.

The cast operator causes the division of one integer variable by another to be performed as a floating-point operation −

#include <stdio.h>

int main() {

   int sum = 17, count = 5;
   double mean;

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

Output

When the above code is compiled and executed, it produces the following result −

Value of mean: 3.400000

It should be noted here that the cast operator has precedence over division, so the value of sum is first converted to type double and finally it gets divided by count yielding a double value.

Type conversions can be implicit which is performed by the compiler automatically, or it can be specified explicitly through the use of the cast operator. It is considered good programming practice to use the cast operator whenever type conversions are necessary.

Rules of Type Promotions

While performing the implicit or automatic type conversions, the C compiler follows the rules of type promotions. Generally, the principle followed is as follows −

  • Byte and short values − They are promoted to int.
  • If one operand is a long − The entire expression is promoted to long.
  • If one operand is a float − The entire expression is promoted to float.
  • If any of the operands is double − The result is promoted to double.

Integer Promotion in C

Integer promotion is the process by which values of integer type "smaller" than int or unsigned int are converted either to int or unsigned int.

Example

Consider an example of adding a character with 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);
}

Output

When the above code is compiled and executed, it produces the following result −

Value of sum: 116

Here, the value of sum is 116 because the compiler is doing integer promotion and converting the value of 'c' to ASCII before performing the actual addition operation.

Usual Arithmetic Conversion

The usual arithmetic conversions are not performed for the assignment operators nor for the logical operators && and ||.

Example

Let us take the following example to understand the 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);
}

Output

When the above code is compiled and executed, it produces the following result −

Value of sum: 116.000000

Here, it is simple to understand that first c gets converted to an integer, but as the final value is double, usual arithmetic conversion applies and the compiler converts i and c into 'float' and adds them yielding a 'float' result.

Advertisements