Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Types of Typecasting operations
2.1.
1. Implicit type casting
2.1.1.
Essential Points about Implicit Conversions
2.2.
2. Explicit type casting
2.2.1.
Example
3.
Frequently Asked Questions
4.
Key Takeaways
Last Updated: Mar 27, 2024

Typecasting

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

When the data is converted from one to another data type, then it's known as typecasting. Typecasting is nothing but converting the kind of data. Using typecasting, the type of the data may be modified but now not the data itself. It is one of the essential ideas introduced in 'C' programming.

You can also read about the jump statement, C Static Function 

Types of Typecasting operations

C programming provides two types of type casting operations:

1. Implicit type casting

Implicit type casting means conversion of data types without losing their original means. Typecasting is essential when you need to change data types without converting the values stored inside the variable.

Implicit type conversion in C occurs automatically when a value is copied to its like-minded data type. in the course of the conversion, strict rules for type conversion are applied. If the operands are of two different data types, then an operand with a lower one is automatically converted right into a higher one. This type of type conversion may be seen in the following example.

#include<stdio.h>
int main()
{
	short x=10; //initializing variable of short data type
	int y; //declaring int variable
	y=x; //implicit type casting
	printf("%d\n",x);
	printf("%d\n",y);
}

Output:

10
10

Essential Points about Implicit Conversions

  • The implicit type of type conversion is also known as standard type conversion. We do no longer require any keyword or unique statements in implicit typecasting.
  • Converting from smaller data types into large ones is likewise called type promotion. In the above example, we also can say that the value of s is promoted to type integer.
  • The implicit type conversion constantly occurs with the compatible data types.

Also see, Floyd's Triangle in C

2. Explicit type casting

In an implicit type conversion, the data type is converted automatically. There are some scenarios wherein we may additionally need to force type conversion. Suppose we've got a variable div that stores the division of operands declared as an int data type.

int result, var1=10, var2=3;
result=var1/var2;

In this case, after the division is completed on variables var1 and var2, the result stored in the variable "result" may be in an integer format. The value stored in the variable "result" loses its meaning on every occasion. It does not don't forget the fraction part, which is usually obtained within the division of numbers.

To force the type conversion in such conditions, we use explicit typecasting.

It requires a type casting operator. The overall syntax for type casting operations is as follows:

(type-name) expression

Example

#include<stdio.h>
int main()
{
	float x = 1.2;
	//int y = x; //Compiler will throw an error for this
	int y = (int)x + 1;
	printf("Value of x is %f\n", x);
	printf("Value of y is %d\n",y);
	return 0;
}

Output:

The value of x is 1.200000
The value of y is 2


You can also read about C dynamic array, Tribonacci Series  and Short int in C Programming

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

1. What is typecasting?

Ans: when the data is converted from one data type to another data type, then it's far known as typecasting. Typecasting is nothing but converting the kind of data. Using typecasting, the best type of the data may be modified but now not the data itself.

2. What are auto-up casting and explicit downcasting?

Ans: An object of sub-class type may be automatically cast to superclass type. That is referred to as an auto-up casting. An object of superclass type has to be explicitly cast to sub-class type. It is known as explicit downcasting.

3. What is a good use for typecasting?

Ans: To allow the division of two integers to return a decimal value.

4. Why can be typecasting be dangerous?

Ans: You might temporarily lose part of the data - such as truncating a float when typecasting to an int. 

Key Takeaways

We learned that typecasting is also called type conversion. Its method of converting one data type into some other. Converting a smaller data type into a larger one is also called type promotion. For more exercise, you can visit interview Experiences of top tech companies for practice.

Live masterclass