Casting in c – Type Casting in C Programming

Casting in c: Type casting in C programming language is a way to force an expression of one data type to another data type. We generally use type casting to convert a variable of one data type to another data type. If we want to perform floating point arithmetic with a integer variable then we have to first type cast integer variable into float. Below is the syntax of caste operator.

(data_type) expression;
  • data_type : It is a valid target data type name.
  • expression : This is a valid C expression, whose value will get converted to data_type.

For Example

int circumference = (int)(2 * 3.142 * radius);
float average = (float)sum/count;
(float)5/2

C Program to show Type Casing of Variables

  • In the below C program, totalMarks/count will produce integer value without type casting.
  • We type casting totalMarks variable to float data type to retain the fractional component of quotient.
  • Variable totalMarks gets converted to float before division.

Type Casting in C Programming

#include <stdio.h>
#include <conio.h>
 
main() {
   int i, totalMarks=0, count=5, marks;
   float averageMarks;
   printf("Enter marks of five subjects\n");
   for(i = 0; i < count; i++){
      scanf("%d", &marks);
   totalMarks += marks;
   }
   /* Calculating Average without Type Casting */
   printf("Average marks without Type casting = %d\n", 
       totalMarks/count);
   /* Calculating Average after Type Casting totalMarks to float */
   printf("Average marks after Type casting = %f\n", 
       (float)totalMarks/count);
    
   getch();
   return 0;
}

Output

Enter marks of five subjects
1 6 2 5 9
Average marks without Type casting = 4
Average marks after Type casting = 4.600000

Automatic Type conversion in Expressions

Casting variables in c: When an expression contains variables and constants of different data types, all of them gets converted to a same data types. Compiler converts the data type of all variables to the highest data type variable in the expression, this is known as type promotion.

Here is the ranking of the data types from lower to higher.

int --> unsigned int --> long --> unsigned long --> long long --> unsigned long long --> float --> double --> long double
integer + float becomes float.
Integer gets promoted to float because lower data type operand get converted to the data type of higher operand.

After the automatic type conversion of operands, each pair of operands is of same data type and the result of each operation is same as the data type of both operand.

For Example
Let us consider four variables i, f and d of data type int, float and double respectively and expression (i*f)/d

  • First integer i is converted to float. and the outcome of i*f is a float value.
  • Then the outcome of i*f which is a float s converted to double and the outcome of (i*f)/d becomes a double.

C Program to show Automatic Type Promotion

C Program to show Automatic Type Promotion

#include <stdio.h>
#include <conio.h>
 
main(){
   int  i = 2;
   float f = 13.5, result;
   /* Integer gets promoted to float before division */
   result = f/i;
   printf("Quotient of %f/%d = %f\n", f, i, result);
    
   getch();
   return 0;
}

Output

Quotient of 13.500000/2 = 6.750000

Points to Remember

  • When we convert lower data type to higher data type we don’t loose any data like conversion of int to long.
  • Data will be truncated when higher data type is converted to lower data type. When double is converted to long, we loose the fractional portion of the data.