C Programming Tutorial - ITDeveloper

ITDeveloper : C Programming

C Programming

Step by Step Tutorials


Share with a Friend

C Type Casting / Type Conversion

In C programming, we can convert the value of one data type (int, float, double, etc.) to another. This process is known as type casting or Type conversion. Let's see an example,

Example:

#include <stdio.h>

int main() {

  int num = 23.56;

  printf("%d", num);

  return 0;

}

Output: 23

Here, we are assigning the double value 23.56 to the integer variable number. In this case, the double value is automatically converted to integer value 23.

This type of conversion is known as implicit type conversion. In C, there are two types of type conversion:

  1. Implicit Conversion
  2. Explicit Conversion

Implicit Type Conversion In C

As mentioned earlier, in implicit type conversion, the value of one type is automatically converted to the value of another type. For example,

#include<stdio.h>

int main() {

  // create a double variable

  double value = 2012.36;

  printf("Double Value: %.2lf\n", value);

  // convert double value to integer

  int num = value;

  printf("Integer Value: %d", num);

  return 0;

}

OUTPUT:

Double Value: 2012.36

Integer Value: 2012

The above example has a double variable with a value 2012.36. Notice that we have assigned the double value to an integer variable.

int num = value;

Here, the C compiler automatically converts the double value 2012.36 to integer value 2012.

Since the conversion is happening automatically, this type of conversion is called implicit type conversion.

Example: Implicit Type Conversion

#include<stdio.h>

int main() {

  // character variable

  char ch = 'a';

  printf("Character Value: %c\n", ch);

  // assign character value to integer variable

  int num = ch;

  printf("Integer Value: %d", num);

  return 0;

}

OUTPUT:

Character Value: A

Integer Value: 65

The code above has created a character variable alphabet with the value 'A'. Notice that we are assigning alphabet to an integer variable.

int num = ch;

Here, the C compiler automatically converts the character 'A' to integer 65. This is because, in C programming, characters are internally stored as integer values known as ASCII Values.

ASCII defines a set of characters for encoding text in computers. In ASCII code, the character 'A' has integer value 65, that's why the character 'A' is automatically converted to integer 65.

Explicit Type Conversion In C

In explicit type conversion, we manually convert values of one data type to another type. For example,

#include<stdio.h>

int main() {

  // create an integer variable

  int num = 35;

  printf("Integer Value: %d\n", num);

  // explicit type conversion

  double value = (double) num;

  printf("Double Value: %.2lf", value);

  return 0;

}

OUTPUT:

Integer Value: 35

Double Value: 35.00

We have created an integer variable named number with the value 35 in the above program. Notice the code,

// explicit type conversion

double value = (double) number;

Here,

  • (double)- represents the data type to which number is to be converted
  • number- value that is to be converted to double type

Example: Explicit Type Conversion

#include<stdio.h>

int main() {

  // create an integer variable

  int num = 97;

  printf("Integer Value: %d\n", num);

  // (char) converts number to character

  char ch = (char) num;

  printf("Character Value: %c", ch);

  return 0;

}

OUTPUT:

Integer Value: 97

Character Value: a

We have created a variable num with the value 97 in the code above. Notice that we are converting this integer to character.

char ch = (char) number;

Here,

  • (char)- explicitly converts number into character
  • number- value that is to be converted to char type