Introduction

The expression in a computer language contains data values of the same or various data kinds. When the expression contains similar datatype values, it is evaluated correctly. However, if the phrase contains two or more different datatype values, they must be transformed to the destination datatype's single datatype. The destination in this case is the location where the final result of that expression is saved. For instance, multiplying an integer data value by a float data value and storing the result in a float variable. In this scenario, the integer value must be transformed to a float value, resulting in a float datatype value.

#include <stdio.h>

int main() {
   

int number = 34.78;

  printf("%d", number);
   
  return 0;
}



// Output: 34

write your code here: Coding Playground

The integer variable number is given the double value 34.78 in this case. That a double value is converted back to integer value 34 in this case.

Types of Conversion

Implicit type conversion is the name given to this type of conversion. There are 2 kinds of type conversion in C:

  1. Implicit type Conversion
  2. Explicit Type conversion

Implicit Type Conversion

As previously stated, implicit type conversion automatically converts the value of one type to the value of some other type. As an example,

#include<stdio.h>

int main() {

  // create a double variable
  double value = 4150.12;
  printf("Double Value: %.2lf\n", value);


// convert double value to integer
  int number = value;

  printf("Integer Value: %d", number);
   
  return 0;
}

Output

Double Value: 4150.12
Integer Value: 4150

A double variable with the value 4150.12 is used in the preceding example. It's worth noting that we've allotted the double value to an integer variable.

int number = value;

The C compiler automatically changes the double value 4150.12 to the integer value 4150 in this case.

Because the conversion occurs automatically, this type of converting is known as implicit type conversion.

write your code here: Coding Playground

Example:

#include<stdio.h>

int main() {
   
  // character variable
  char alphabet = 'a';
  printf("Character Value: %c\n", alphabet);


// assign character value to integer variable
  int number = alphabet;

  printf("Integer Value: %d", number);
   
  return 0;
}

Output

Character Value: a
Integer Value: 97

The preceding code generated a character variable alphabet with value 'a.' It's worth noting that we're attributing alphabet to an integer variable.

int number = alphabet;

The C compiler automatically changes the character 'a' to integer 97 in this case. This is because characters in C programming are internally saved as integer values known as ASCII Values.

ASCII is a set of characters used in computers to encode text. Because the character 'a' has the integer value 97 in ASCII code, it is easily transformed to integer 97.

write your code here: Coding Playground

Explicit type Conversion In C

We manually convert value systems of one type of data to another in explicit type conversion.

Example

#include<stdio.h>

int main() {

  // create an integer variable
  int number = 35;
  printf("Integer Value: %d\n", number);
 

// explicit type conversion
  double value = (double) number;

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

Output

Integer Value: 35
Double Value: 35.00

In the preceding programme, we declared an integer variable named number with the value 35. Take note of the code.

// explicit type conversion
double value = (double) number;

(double) - the data type to which the number is to be converted number

number - the value to be converted to double type

write your code here: Coding Playground

Example:

#include<stdio.h>

int main() {
   
  // create an integer variable
  int number = 97;
  printf("Integer Value: %d\n", number);


// (char) converts number to character
  char alphabet = (char) number;

  printf("Character Value: %c", alphabet);
 
  return 0;
}

Output

Integer Value: 97
Character Value: a

In the code above, we created a variable with the value 97. It's worth noting that we're converting this integer to a character.

char alphabet = (char) number;

(char) - explicitly converts a number to a character value number

number - the value to be converted to char type

write your code here: Coding Playground

Data loss In Type Conversion

When we transformed a double type value to an integer type in previous examples, the data after the decimal point was lost.

#include<stdio.h>

int main() {

  // create a double variable
  double value = 4150.12;
  printf("Double Value: %.2lf\n", value);
   
  // convert double value to integer
  int number = value;
  printf("Integer Value: %d", number);
   
  return 0;
}

Output

Double Value: 4150.12
Integer Value: 4150

write your code here: Coding Playground

4150 is the data here. 12 is equal to 4150. Data after the decimal point,.12, is lost in this conversion.

This happens because double is a bigger data data type (8 bytes) than data type int(4 bytes), and data loss occurs when we convert data from one type to another.

Similarly, there's a data type hierarchy in C programming. According to the hierarchy, data is lost when a higher data type is transformed to a lower data type, but no data is lost when a lower data type is transformed to a higher data type.

Here,

  • Data loss occurs when a long double type is transformed to a double type.
  • There is no data loss if char is transformed to int.

Conclusion

We hope you found our post interesting and learned about Type Conversion in C in your own code.