0% found this document useful (0 votes)
23 views9 pages

16 Type Casting

Uploaded by

555bsyadav555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views9 pages

16 Type Casting

Uploaded by

555bsyadav555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Type Casting

What is type casting?


➢ Converting one data type to another data type is called type conversion or type
casting.
➢ Type casting is used when the left operand (variable) and right operand (variable,
constant, expression) of the assignment operator are not of the same data type.
➢ For example, if we want to convert long data type into int data type, then we use
type cast operator.
Type casting is of two types:
1. Implicit Type Casting:-
2. Explicit Type Casting:-

1. Implicit Type Casting:-


1. When no extra effort is required to assign a constant or variable of one data
type to a variable of another data type, it is called implicit type casting.
2. There is no such rule to convert lower data type to upper data type in C
language.
3. Here upper data type means larger size (in bytes) and lower data type means
smaller size (in bytes).
4. In C language, the value of the lower data type is easily assigned to the upper
data type variable and the value of the upper data type to the variable of the
lower data type.
5. If we try to put the value of upper data type in lower data type variable then
some value of upper data type may be lost.
# include <stdio.h>
int main( )
{
float a ;
int x = 6, y = 4 ;
a=x/y;
printf ( "Value of a = %f\n", a ) ;
return 0 ;
}

Ex.:- Assigning a literal or variable


1. Upper Data Type into Lower Data Type
int i = 98.17; // Implicit Type Casting/ i = 98 / .17 will be lost
char ch = 97.89; // Implicit Type Casting/ ch = 97 or ‘a’ / .89 will be lost
flaot f = 45.67;
int i = f; // i = 45 and .67 will be lost
flaot f = 65.67;
char ch = f; // ch = 65 or ‘A’ and .67 will be lost

2. Lower Data Type into Upper Data Type


float f = 98; // Implicit Type Casting/ f = 98.000000
double d = ‘A’; // Implicit Type Casting/ d = 65.000000
int i = 10;
double d = i; // d = 10.000000
char ch = ‘a’;
float f = ch; // f = 97.000000

Ex.:- Implicit type casting within expressions.


➢ If an expression contains literals and variables of different data types, then the
result will always be according to the upper literal or the corresponding variable’s
data type.

Operation Result

5/2 2

5.0 / 2 2.500000

5 / 2.0 2.500000

5.0 / 2.0 2.500000

int i = 2/9; 0
float f = 2/9; 0.000000

int i = 2.0/9; 0

int i = 2.0/9.0; 0

int i = 9/2; 4

int i = 9.0/2; 4

int i = 9/2.0; 4

int i = 9.0/2.0; 4

Here are some common scenarios where implicit type casting occurs in C:
Assignment: When you assign a value of one data type to a variable of another data type,
C may perform implicit type casting to make the assignment compatible.
For example:
int integerVar = 42;
double doubleVar = integerVar; // Implicit casting from int to double

Arithmetic Operations: In expressions involving different data types, C will automatically


promote them to a common data type before performing the operation. The promotion
generally follows a hierarchy, with the goal of avoiding data loss.
For example:

int num1 = 5;
double num2 = 2.5;
double result = num1 + num2; // Implicit casting of int to double

Comparison Operators: When comparing values of different data types, C will perform
implicit casting to make the comparison valid.
For example:

int intValue = 5;
double doubleValue = 5.0;
if (intValue == doubleValue) {
// Implicit casting of int to double for comparison
}
Function Arguments: When you pass arguments to a function, and the function expects a
different data type than what you're passing, C may perform implicit type casting.
For example:

void printDouble(double value) {


printf("%lf\n", value);
}
int intValue = 10;
printDouble(intValue); // Implicit casting of int to double

Note: It's important to note that implicit type casting can lead to unexpected behavior
and data loss if you're not careful. Therefore, it's essential to be aware of the C language's
rules for implicit type casting and make explicit type conversions (using casting operators
like (type)) when necessary to ensure the desired behavior and avoid potential issues.

2. Explicitly Type Casting:-


➢ As we know that there are no rules for type casting in C programming language, in
most cases, type casting is done implicitly. But there is a possibility of loss of data
in this approach. For this we need type casting.
➢ Explicit type casting is used with expressions in maximum cases.

In C, explicit type casting is the process of converting a value from one data type to
another by using casting operators. Explicit type casting allows you to control and
specify the conversion you want, and it's especially useful when you need to convert
between incompatible data types. The casting operator in C is denoted by enclosing
the target data type in parentheses before the value or expression you want to
convert.

Example:
#include <stdio.h>

int main() {
float a;
int x = 6, y = 4;
a = (float)x / y; // Explicitly cast x to float for division
printf("Value of a = %f\n", a);
return 0;
}

Here is the format of a typecast:

(dataType)value
The dataType can be any C data type, such as int or float. The value is any variable,
literal, or expression. Suppose that age is an integer variable that holds 6. The
following converts age to a float value of 6.0:
(float)age;

Note: variable—age is changed only temporarily for this one calculation. Everywhere in
the program that age is not explicitly typecast, it is still an int variable.

Here are a few examples of explicit type casting:

Casting from int to double:


int intValue = 42;
double doubleValue = (double)intValue; // Explicitly cast int to double

Casting from double to int (with truncation):

double doubleValue = 3.14159;


int intValue = (int)doubleValue; // Explicitly cast double to int (truncation)

Casting from float to int:

float floatValue = 3.75;


int intValue = (int)(floatValue + 0.5); // Explicitly cast float to int with rounding

Casting from char to int:


char charValue = 'A';
int intValue = (int)charValue; // Explicitly cast char to int (ASCII value)

Casting from a larger integer type to a smaller one:

long longValue = 1000000;


int intValue = (int)longValue; // Explicitly cast long to int (possible data loss)

Casting to ensure correct comparison between different data types:

int intValue = 42;


double doubleValue = 42.0;
if ((double)intValue == doubleValue) {
// Explicitly cast int to double for comparison
}

Typecast an entire expression:

This statement typecasts the result of an expression before assigning it to a variable:

value = (float)(number - 10 * yrsService);

Operation Result

float f = 2/9; 0.000000

float f = (float)2/9; 0.222222

float f = 2.0/9; 0.222222

float f = 9/2; 4.000000


float f = (float)9/2; 4.500000

Note: Explicit type casting should be used with caution because it allows you to perform
potentially unsafe conversions. When using explicit casting, be aware of the potential for
data loss, precision issues, or undefined behavior if the conversion is not meaningful or
compatible.

You might also like