Type Casting in C

Share Your Love

What is typecasting?

Typecasting can be referred to as the conversion of one data type to another data type. It is also known as data conversion. There are two types of typecasting in C:

  • Implicit type casting
  • Explicit type casting 

Let us define them one by one.

Implicit Type Casting:

This type of conversion takes place automatically when a value is copied to its compatible data type. It is actually defined as the conversion of data without losing its actual significance. Rules are followed strictcly, such as lower oder data type operands are automatically converted into high oder data type.

For Example:

#include<stdio.h>
int main(){
	short a=10; 
	int b;
	b=a;
	printf("%d\n",a);
	printf("%d\n",b);
	return 0;
}

Here at first a variable of short data type is initialised with 10, then the value of that variable is passed to a variable having integer data type. The statement of initialising the value as b=a is known as implicit typecasting.

Output:

pMh5U9kzIRL3L3UQXv 4cXFbSrREU2vwSbD1YK4kls tvbqXIPa lXRiWXPHZlghvZLQYHluj4aj v4VigIM74h6aNLWeXDatTtULUnVPhKoYvtvr 0e4rG2LY1jIhdP2UfZnw

Points to remember about implicit type casting:

  • Converting from smaller data type into a larger data type is called type promotion.
  • The implicit type conversion always takes place between compatible data types.
  • Converting float to an integer will truncate the fraction part, hence losing the meaning of the value in the variable.
  • Converting double to float will round up the digits.
  • Converting a long integer to an integer will cause the dropping of excess high order bits.

Explicit Type Casting:

In implicit type conversion, the data type is converted automatically. In some cases force type conversion are made, such forced type conversion comes under explicit type casting.

Syntax For Explicit Type Casting:

(type name) expression;

In this case the type name is initialized with the data types present in C, where as the expression is initialised with expressions, constants or variables.

For example:

#include<stdio.h>
int main()
{
	float a = 1.2;
	int b = (int)a;
	printf("Value of a is %f\n", a);
	printf("Value of b is %d\n",b);
	return 0;
}

Here, the variable a of float data type is initialized and the it is forced type casted to the variable b of integer data type. This force type casting is called explicit type casting.

Output:

PmVBmFmzMlyyyntHyrL gnh84 joK K qOKJHy UMv53zHbxnSi9scHpjO FxPzcZkqS2E57J9aOPbwoVyaz mzRzy5AqlECYDPhmc 7j

Arithmetic Conversion Hierarchy:

The compiler first proceeds with promoting a character to an integer. If the operands still have different data types, then they are converted to the highest data type that appears in the following hierarchy chart:

type casting goals
Type Cast In C

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Avatar photo
Soham Malakar

Hello my name is Soham Malakar am currently pursuing BCA

Coding is one of my favorite hobbies.
I love to code and an enthusiast to learn different types of programming languages.
I love to get intoxicated in the world of technology and video games.
A quick learner possessing better results.

Articles: 40

Newsletter Updates

Enter your email address below to subscribe to our newsletter