Type Modifiers and Casting
Type Modifiers and Casting
}
Range of data types
Output
Type casting
• Essential when the values of the variables are
to be converted from one type to another
type.
• Type casing- Explicit, Implicit
Type casting
Explicit typecasting
• Compiler is instructed to do type conversion
using typecast operator
• Syntax:
data type name (expression)
Example
# include<iostream>
using namespace std;
main()
{
cout<< int (17.78)<<endl;
cout<<(int) 17.78<<endl;
}
Output
Implicit typecasting
• When the expression contains different types
of data items, implicit conversion is carried
out.
• Promotion- Variable of lower data type is
converted to higher data type
• Demotion – Variable of higher type is
converted to lower type
Example
# include <iostream>
using namespace std;
main()
{
int j=2.54;
int k='A';
char c=87.5;
cout<<"j="<<j<<endl;
cout<<"k="<<k<<endl;
cout<<"c="<<c<<endl;
}
Output