说明符用于改变基本内建类型的含义并把他们扩展成一个更大的集合
4个说明符:long short signed unsigned
long 和 short都有最大最小值
signed unsigned修饰符告诉整数类型和字符的符号位
unsigned不保存符号,有一个多余的位可用,比signed数大一倍。
4个说明符:long short signed unsigned
long 和 short都有最大最小值
signed unsigned修饰符告诉整数类型和字符的符号位
unsigned不保存符号,有一个多余的位可用,比signed数大一倍。
不同机器、操作系统、编译器运行程序的结果可能不太。
//: C03:Specify.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Demonstrates the use of specifiers
#include <iostream>
using namespace std;
int main() {
char c;
unsigned char cu;
int i;
unsigned int iu;
short int is;
short iis; // Same as short int
unsigned short int isu;
unsigned short iisu;
long int il;
long iil; // Same as long int
unsigned long int ilu;
unsigned long iilu;
float f;
double d;
long double ld;
cout
<< "\n char= " << sizeof(c)
<< "\n unsigned char = " << sizeof(cu)
<< "\n int = " << sizeof(i)
<< "\n unsigned int = " << sizeof(iu)
<< "\n short = " << sizeof(is)
<< "\n unsigned short = " << sizeof(isu)
<< "\n long = " << sizeof(il)
<< "\n unsigned long = " << sizeof(ilu)
<< "\n float = " << sizeof(f)
<< "\n double = " << sizeof(d)
<< "\n long double = " << sizeof(ld)
<< endl;
getchar();
} ///:~
输出
char= 1
unsigned char = 1
int = 4
unsigned int = 4
short = 2
unsigned short = 2
long = 4
unsigned long = 4
float = 4
double = 8
long double = 8