C Type Casting

In C language type casting or type conversion is a way to convert one data type into another. There are two types of type conversion. They are following:

  1. Implicit type conversion
  2. Explicit type conversion


Implicit type conversion

C automatically converts one type of data into another. This automatic conversion is known as implicit conversion. Such as if you put an integer value into a floating type variable the compiler will convert the integer value into float automatically. For example:

Example

  1. int a = 53;
  2. float b;
  3. b = a;


Explicit type conversion

The way to force a type conversion it is called explicit conversion. The syntax of explicit conversion is following:

Syntax

  1. (type_name) expression

Here type_name is one of the standard C data types. The expression may be a constant, variable or an expression. An example of explicit conversion is given below:

Example:

  1. #include <stdio.h>
  2. int main() {
  3. float num;
  4. num = (float) 27/7;
  5. printf("%f", num);
  6. return 0;
  7. }

Output:

3.857143