Type Casting In C Programming Language

Type casting refers to converting one data type variable into another. If it makes sense, the compiler will automatically change one kind of data into another. For example, if you assign an integer value to a variable with a floating-point, the compiler will convert the int to a float. Casting allows you to make an explicit conversion of this type, or force it when it would not normally occur.

What is Type Casting or Type Conversion?

Converting an expression of a given type into a different type is known as casting type. Typecasting is more use in c programming language.

Converting lower data type into higher data type is best practise here to prevent data loss.

When converting the higher data form to lower, data will be truncated. For example, if a float is converted to int, it will lose data that is present after the decimal point.

Type conversion in c programming language can be classified in two types:

Implicit type conversion

Implicit type casting means that data types are transformed without losing their original purpose. When you want to change data types without affecting the meaning of the values stored within the variable, this form of typecasting is important.

Implicit type conversion occurs when a value is copied to its compatible data type automatically. Strict rules for type conversion shall be enforced during conversion. If the operands are of two distinct data types, the lower data type of an operand is automatically transformed into a higher data type. In the following example, this form of type conversion can be seen.

Implicit type casting


Explicit type conversion

This is known as an explicit conversion, where the user manually switches data from one type to another. This conversion type is also known as type casting.

Explicit type casting

The value of X is 2.300000
The value of Y is 2