0% found this document useful (0 votes)
60 views

Type Modifiers and Casting

This document discusses C++ type modifiers, which are keywords that change a base data type to a new data type. It covers the type modifiers signed, unsigned, short, and long, and which modifiers can be applied to int, char, and double data types. It also provides examples of declaring variables with type modifiers and outputting their values. It discusses concepts like wrapping around that can occur when a variable's value moves out of range. Finally, it covers type casting, including explicit casting using cast operators and implicit casting that can occur when variables of different types are used in expressions together.

Uploaded by

GAMES TECH
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)
60 views

Type Modifiers and Casting

This document discusses C++ type modifiers, which are keywords that change a base data type to a new data type. It covers the type modifiers signed, unsigned, short, and long, and which modifiers can be applied to int, char, and double data types. It also provides examples of declaring variables with type modifiers and outputting their values. It discusses concepts like wrapping around that can occur when a variable's value moves out of range. Finally, it covers type casting, including explicit casting using cast operators and implicit casting that can occur when variables of different types are used in expressions together.

Uploaded by

GAMES TECH
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/ 16

Type Modifiers

Type modifiers- keywords


• Signed, unsigned, short and long – type
modifier keywords
• Changes base data type to new data type.
• int- all type modifiers are applicable
• char – signed and unsigned are applicable
• double- long is applicable
• With type modifiers if data type is not used
then by default int data type is assumed
Example
# include <iostream>
using namespace std;
main()
{
short t=1;
long k=54111;
unsigned u=10;
signed j=-10;
cout<<"\n t=" <<t;
cout<< "\n k="<<k;
cout<<"\n u="<<u;
cout<<"\n j="<<j;
}
Output
Wrapping around
• When the value of the variable moves out of
range, compiler does not flag an error.
• Wrapping around occurs
• Value after wrapping: difference between the
range and value of variable out of range
Exaxmple
# include <iostream>
using namespace std;
main()
{
unsigned short int x = 65536; cout<<"x="<<x;

}
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

You might also like