Typecasting in C Language
×


Typecasting in C Language

116

 Typecasting in C is similar to reshaping mud into different forms.

It's a method to convert values from one data type to another, ensuring they fit together smoothly.

 Just as molding clay into various shapes, typecasting ensures different data types can cooperate effectively.

Types of Typecasting in C 


1 Implicit Typecasting (Automatic)

 Implicit typecasting is automatically performed by the Compiler.

The compiler automatically converts data types as necessary during compilation.

For Example:

Converting int to float during an arithmetic operation.


2 Explicit Typecasting (Manual)

Explicit typecasting is performed by the user using a casting operator.

 In explicit typecasting, if you want to convert a value from one type to another, you need to specify the desired type within parentheses before using the value.

 This tells the compiler to treat the value as the specified type during the conversion process.

For Example:

explicitly converts an integer to a float.


Syntax of Typecasting in C Language

(new data type)Expression;

Arithmetic conversation in C 

Example:

#include<stdio.h>
int main()
{
	int A = 3;
	float B = 7.7;
	int Total;
	Total = (int)B + A; // Typecasting float B in int
	printf("Total = %d\n", Total);
	return 0;
}
Output:

Total = 10
In this code, B is typecasted from float to int to perform addition with A.

Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments