TYPE CASTING
What is Type Casting
The process of converting one predefined datatype into another is called type casting.
Syntax:-
(datatype)value;
C programming provide 2 types of type casting.
Note:- If we are put a char (character datatype) so its print is ASCII Code.
1.Implicit Type Casting:-In C language datatype can directly convert into one to another by compiler without user interface is known as implicit type casting. In this casting the lower data type is automatically converted into higher datatype. It is also known as promotion of datatype.
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x= 10; //integer x
char y= 'a'; //character y, ASCII value of 'a' is 97
// y implicitly converted to int .ASCII
x =x+y;
float z =x + 2.0; // x is implicitly converted to float
printf("x = %d, z= %f", x, z);
getch();
}
Output:
x=107, y=109.00000
2.Explicit Type Casting:- In this type of type casting this is not perform by compiler but explicit specify by user.And explicit type casting higher datatype is converted into lower datatype.
Example:
#include<stdio.h>#include<conio.h>
void main()
{
double x=2.2;
int sum=(int) x+3; //Explicit casting from double to int
printf("sum =%d",sum);
getch();
}
output:
sum =5
WAITING FOR NEXT TOPIC
What is Type Casting
The process of converting one predefined datatype into another is called type casting.
Syntax:-
(datatype)value;
C programming provide 2 types of type casting.
Note:- If we are put a char (character datatype) so its print is ASCII Code.
1.Implicit Type Casting:-In C language datatype can directly convert into one to another by compiler without user interface is known as implicit type casting. In this casting the lower data type is automatically converted into higher datatype. It is also known as promotion of datatype.
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x= 10; //integer x
char y= 'a'; //character y, ASCII value of 'a' is 97
// y implicitly converted to int .ASCII
x =x+y;
float z =x + 2.0; // x is implicitly converted to float
printf("x = %d, z= %f", x, z);
getch();
}
Output:
x=107, y=109.00000
2.Explicit Type Casting:- In this type of type casting this is not perform by compiler but explicit specify by user.And explicit type casting higher datatype is converted into lower datatype.
Example:
#include<stdio.h>#include<conio.h>
void main()
{
double x=2.2;
int sum=(int) x+3; //Explicit casting from double to int
printf("sum =%d",sum);
getch();
}
output:
sum =5
WAITING FOR NEXT TOPIC
Comments
Post a Comment