程序如下:
int main(/*int argc, char *argv[]*/)
{
cout << "type\t\t\t" << "size" << endl
<< "bool\t\t\t" << sizeof(bool) << endl
<< "char\t\t\t" << sizeof(char) << endl
<< "signed char\t\t" << sizeof(signed char) << endl
<< "unsigned char\t\t" << sizeof(unsigned char) << endl
<< "wchar_t\t\t\t" << sizeof(wchar_t) << endl
<< "short\t\t\t" << sizeof(short) << endl
<< "signed short\t\t" << sizeof(signed short) << endl
<< "unsigned short\t\t" << sizeof(unsigned short) << endl
<< "int\t\t\t" << sizeof(int) << endl
<< "signed int\t\t" << sizeof(signed int) << endl
<< "unsigend int\t\t" << sizeof(unsigned int) << endl
<< "long\t\t\t" << sizeof(long) << endl
<< "sigend long\t\t" << sizeof(signed long) << endl
<< "unsigned long\t\t" << sizeof(unsigned long) << endl
<< "float\t\t\t" << sizeof(float) << endl
<< "double\t\t\t" << sizeof(double) << endl
<< "long double\t\t" << sizeof(long double) << endl;
return 0;
}
結果如下:
type size
bool 1
char 1
signed char 1
unsigned char 1
wchar_t 2
short 2
signed short 2
unsigned short 2
int 4
signed int 4
unsigend int 4
long 4
sigend long 4
unsigned long 4
float 4
double 8
long double 8
注:
size爲字節的長度,而一字節爲8位。
以short型爲例,上面輸出short型爲2字節,那就是16位,而存儲的數據範圍爲 -2^15 ~ 2^15-1。