Thursday 6 April 2023

Type Conversion and Type Casting in C

Type conversion and type casting are two related concepts in C that involve changing the data type of a variable.

Type Conversion: Type conversion, also known as implicit conversion, occurs when C automatically converts one data type to another data type when it's required by the context. For example, if you assign an integer value to a floating-point variable, C will automatically convert the integer to a floating-point number.

Here's an example of type conversion in C:

int a = 5;
float b = a; // Implicit conversion from int to float
 

In this example, C automatically converts the integer value 5 to a floating-point value and assigns it to the variable b.

Type Casting: Type casting, also known as explicit conversion, is when the programmer manually converts a variable from one data type to another data type using a cast operator. The cast operator is denoted by placing the desired type in parentheses before the variable to be cast.

Here's an example of type casting in C:

float a = 3.14;
int b = (int) a; // Explicit conversion from float to int using type casting
 

In this example, the floating-point value 3.14 is explicitly cast to an integer value using the (int) cast operator. The resulting integer value is assigned to the variable b.

Type casting can be useful for manipulating data types in certain situations, but it can also lead to errors if the cast is incorrect or if the data type cannot be accurately represented in the target type.

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top