0% found this document useful (0 votes)
7 views19 pages

5 Typecasting

The document explains C-typecasting and type conversion, detailing implicit and explicit typecasting. Implicit typecasting automatically converts lower data types to higher ones, while explicit typecasting requires a type casting operator to force conversion. It also highlights important points regarding data type compatibility and potential loss of value meaning during conversions.

Uploaded by

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

5 Typecasting

The document explains C-typecasting and type conversion, detailing implicit and explicit typecasting. Implicit typecasting automatically converts lower data types to higher ones, while explicit typecasting requires a type casting operator to force conversion. It also highlights important points regarding data type compatibility and potential loss of value meaning during conversions.

Uploaded by

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

C-Typecasting / Type

Conversion
int x=1, z;
float y=2.3,
z =x+y;
z=x+y; //z=3
int+float=float
int+int=int
float/int =float
int/float=float
C-Typecasting / Type
Conversion
Definition:
Typecasting is converting one data type into another one. It is
also called as data conversion or type conversion.

Two type of type conversion


• Implicit
• Explicit
• Implicit typecasting

If the operands are of two different data types, then an operand


having lower data type is automatically converted into a higher data
type.
•Implicit typecasting In the 6th line, we have
assigned the value of
1. #include<stdio.h> variable a to the
2. int main() variable b.
3. {
4. short a=1; //1 is occupied in 16 bits
5. int b; //declaring int variable 32 bits Implicit type
6. b=a; //implicit type casting ,1 is occupying 32 bits conversion is
7. printf("%d\n",a); performed as the
8. printf("%d\n",b);
value from variable a
9. }
which is of short data
type is copied into the
variable b which is of
an int data type.
•Implicit typecasting Output: 108

Converting Character to Int


1. #include <stdio.h>
2. main()
3. {
4. int number = 1;
5. char character = 'k'; /*ASCII value is 107 */
6. int sum;
7. sum = number + character; //int=int+char  int=int
8. printf("Value of sum : %d\n", sum );
9. }
•Implicit typecasting Output: 108.000000

Converting Character to Int


1. #include <stdio.h>
2. main()
3. {
4. int number = 1;
5. char character = 'k'; /*ASCII value is 107 */
6. float sum;
7. sum = number + character; //float=int+char  float =int
8. printf("Value of sum : %f\n", sum );
9. }
• Arithmetic Conversion Hierarchy
The compiler first proceeds with promoting a character to an integer. If the operands
still have different data types, then they are converted to the highest data type that
appears in the excel sheet .
(refer excel sheet named datatypes)

long double
double
float
long int
Int
Short int
char
• Implicit typecasting: Promotion and Demotion
Promotion Demotion
1. #include <stdio.h> 1. #include <stdio.h>
2. int main() 2. int main()
3. { 3. {
4. printf("Hello World\n"); 4. printf("Hello World\n");
5. int a=10; 5. int a;
6. float c; 6. float c=10.23;
7. c=a; //float=int 7. a=c; //int=float
8. printf("a=%d, \t c=%f",a,c); 8. printf("a=%d, \t c=%f",a,c);
9. return 0; 9. return 0;
10.} 10.}

Output: Output:
Hello World Hello World
a=10 c=10.000000 a= 10 c=10.23
• Important points about implicit typecasting

• Implicit type of type conversion is also called as standard type


conversion. We do not require any keyword or special
statements in implicit type casting.
• Converting from smaller data type into larger data type is also
called as type promotion. In the above example, we can also
say that the value of s is promoted to type integer.
• The implicit type conversion always happens with the
compatible data types.
• Some data types like char , short int take less number of bytes
than int, these data types are automatically promoted
to int or unsigned int when an operation is performed on them. This is
called integer promotion. For example no arithmetic calculation
happens on smaller types like char, short and enum. They are first
converted to int or unsigned int, and then arithmetic is done on them.
If an int can represent all values of the original type, the value is
converted to an int . Otherwise, it is converted to an unsigned int.
• 7111
• 81000
• 91001

• 65535 ----16 1’s


• 65536---- 1 16 0’s
• 65537-----1 150’s 1
• Important points about implicit typecasting
• We cannot perform implicit type casting on the data types
which are not compatible with each other such as:
i. Converting float to an int will truncate the fraction part
hence losing the meaning of the value.
ii. Converting double to float will round up the digits.
iii. Converting long int to int will cause dropping of excess
high order bits.
• In all the above cases, when we convert the data types, the
value will lose its meaning. Generally, the loss of meaning of
the value is warned by the compiler.
• Important points about implicit typecasting

{ {
int a; int a;
short b; short b;
b=65535; //max value short can hold a=65537; //a value 1 more that max value short can hold
a=b; 32 bits=(15bits 0)(1 bit 1)(16 bits 0)00000….1000…0001
printf(“a=%d”, a); b=a;
} printf(“b=%d”, b);
}

Note: short = 2 bytes _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


int = 4 bytes 15 (0) 1(1) 15(0) 1(1)
Output:
Output: 1
• Explicit typecasting
• In implicit type conversion, the data type is converted automatically. There
are some scenarios in which we may have to force type conversion. Suppose
we have a variable div that stores the division of two operands which are
declared as an int data type.

int result, var1=10, var2=3;


result = var1/var2;
• In this case, after the division performed on variables var1 and var2 the result
stored in the variable "result" will be in an integer format. Whenever this
happens, the value stored in the variable "result" loses its meaning because it
does not consider the fraction part which is normally obtained in the division
of two numbers.
• Explicit typecasting

To force the type conversion in such situations, we use explicit type


casting.
It requires a type casting operator. The general syntax for type casting
operations is as follows:
(type-name)
expression

Here,
•The type name is the standard 'C' language data type.
•An expression can be a constant, a variable or an actual expression
• Explicit typecasting
1. #include<stdio.h> Output:
2. int main()
3. {
4. float a = 3.2;
5. int b = a; //warning
6. int b = (int)a + 1;
7. printf("Value of a is %f\n",
a);
8. printf("Value of b is %d\
n",b);
9. return 0;
10.}
1.We have initialized a variable 'a' of type float.
• Explicit typecasting 2.Next, we have another variable 'b' of integer
data type. Since the variable 'a' and 'b' are of
1. #include<stdio.h> different data types, 'C' won't allow the use of
2. int main() such expression and it will raise an error. In some
versions of 'C,' the expression will be evaluated
3. { but the result will not be desired.
4. float a = 1.2; 3.To avoid such situations, we have typecast the
variable 'a' of type float. By using explicit type
5. //int b = a; //warning or error casting methods, we have successfully converted
float into data type integer.
6. int b = (int)a + 1; 4.We have printed value of 'a' which is still a float
7. printf("Value of a is %f\n", 5.After typecasting, the result will always be an
integer 'b.'
a);
8. printf("Value of b is %d\
n",b); Output:
9. return 0;
10.}
Explicit typecasting - Example
int a=5, b=2;
float c;
Statements Output
(i) c=a/b; int/int=int c = 2.000000

(ii)c= (float)a/b; c=
float/int=float 2.5
(iii)c=(float)(a/b) c =2.000000
(float)(5/2)
(float) (2)
(iv)c=a/(float)b c =2.5
int/float = float

You might also like