#include
<
iostream
>
using namespace std;
void main()
{
cout<<"size of bool: "<<sizeof(bool)<<" bytes"<<endl;
cout<<"size of char: "<<sizeof(char)<<" bytes"<<endl;
cout<<"size of unsigned char: "<<sizeof(unsigned char)<<" bytes"<<endl;
cout<<"size of wchar_t: "<<sizeof(wchar_t)<<" bytes"<<endl;
cout<<"size of short: "<<sizeof(short)<<" bytes"<<endl;
cout<<"size of int: "<<sizeof(int)<<" bytes"<<endl;
cout<<"size of unsigned int/unsigned: "<<sizeof(unsigned)<<" bytes"<<endl;
cout<<"size of size_t: "<<sizeof(size_t)<<" bytes"<<endl;
cout<<"size of long: "<<sizeof(long)<<" bytes"<<endl;
cout<<"size of unsigned long: "<<sizeof(unsigned long)<<" bytes"<<endl;
cout<<"size of float: "<<sizeof(float)<<" bytes"<<endl;
cout<<"size of double: "<<sizeof(double)<<" bytes"<<endl;
cout<<"size of long double: "<<sizeof(long double)<<" bytes"<<endl;
//out of boundrary testing
//warning C4305: '=' : truncation from 'const int' to 'unsigned char'
//warning C4309: '=' : truncation of constant value
unsigned char ch;
ch=333;
cout<<"Truncation from const int to unsigned char, 333 will become: "<<(int)ch<<endl;
}
using namespace std;
void main()
{
cout<<"size of bool: "<<sizeof(bool)<<" bytes"<<endl;
cout<<"size of char: "<<sizeof(char)<<" bytes"<<endl;
cout<<"size of unsigned char: "<<sizeof(unsigned char)<<" bytes"<<endl;
cout<<"size of wchar_t: "<<sizeof(wchar_t)<<" bytes"<<endl;
cout<<"size of short: "<<sizeof(short)<<" bytes"<<endl;
cout<<"size of int: "<<sizeof(int)<<" bytes"<<endl;
cout<<"size of unsigned int/unsigned: "<<sizeof(unsigned)<<" bytes"<<endl;
cout<<"size of size_t: "<<sizeof(size_t)<<" bytes"<<endl;
cout<<"size of long: "<<sizeof(long)<<" bytes"<<endl;
cout<<"size of unsigned long: "<<sizeof(unsigned long)<<" bytes"<<endl;
cout<<"size of float: "<<sizeof(float)<<" bytes"<<endl;
cout<<"size of double: "<<sizeof(double)<<" bytes"<<endl;
cout<<"size of long double: "<<sizeof(long double)<<" bytes"<<endl;
//out of boundrary testing
//warning C4305: '=' : truncation from 'const int' to 'unsigned char'
//warning C4309: '=' : truncation of constant value
unsigned char ch;
ch=333;
cout<<"Truncation from const int to unsigned char, 333 will become: "<<(int)ch<<endl;
}
输出结果:
size of bool: 1 bytes
size of char: 1 bytes
size of unsigned char: 1 bytes
size of wchar_t: 2 bytes
size of short: 2 bytes
size of int: 4 bytes
size of unsigned int/unsigned: 4 bytes
size of size_t: 4 bytes
size of long: 4 bytes
size of unsigned long: 4 bytes
size of float: 4 bytes
size of double: 8 bytes
size of long double: 8 bytes
Truncation from const int to unsigned char, 333 will become: 77
注解:
1. wchar_t类型用于扩展字符集,如汉字和日语,这些字符不能用单个char表示
2. 一般,short类型为半个机器字(word)长,int类型为一个机器字长,long类型为一个或两个机器字长
3. 在32位机中,int类型和long类型的字长通常是相同的
4. bool类型表示真值true和假值false,0值类型代表false,任何非0的值都代表true
5. 除bool类型外,整型可以是带符号的(signed)也可以是无符号的(unsigned)
6. int,short,long都默认为带符号型
7. 对于unsigned类型来说,编译器对取值求模,取所得值