计算机内存的基本单元是位(bit)。字节(byte)通常指的是8位的内存单元,字节是描述计算机内存量的度量单位。
注意sizeof并不是一个函数。
// limits.cpp -- some integer limits #include <iostream> #include <climits> // use limits.h for older systems int main() { using namespace std; int n_int = INT_MAX; // initialize n_int to max int value short n_short = SHRT_MAX; // symbols defined in climits file long n_long = LONG_MAX; long long n_llong = LLONG_MAX; // sizeof operator yields size of type or of variable cout << "int is " << sizeof (int) << " bytes." << endl; //对于类型名用小括号括起来 cout << "short is " << sizeof n_short << " bytes." << endl; cout << "long is " << sizeof n_long << " bytes." << endl; cout << "long long is " << sizeof n_llong << " bytes." << endl; cout << endl; cout << "Maximum values:" << endl; cout << "int: " << n_int << endl; cout << "short: " << n_short << endl; cout << "long: " << n_long << endl; cout << "long long: " << n_llong << endl << endl; cout << "Minimum int value = " << INT_MIN << endl; cout << "Bits per byte = " << CHAR_BIT << endl; return 0; }输出结果:
int is 4 bytes. short is 2 bytes. long is 4 bytes. long long is 8 bytes. Maximum values: int: 2147483647 short: 32767 long: 2147483647 long long: 9223372036854775807 Minimum int value = -2147483648 Bits per byte = 8 climits库中的符号常量 常量名称含义CHAR_BITchar 的位数CHAR_MAXchar 的最大值CHAR_MINchar 的最小值SCHAR_MAXsigned char 的最大值SCHAR_MINsigned char 的最小值UCHAR_MAXunsigned char 的最大值SHRT_MAXshort 的最大值SHRT_MINshort 的最小值USHRT_MAXunsigned short 的最大值INT_MAXint 的最大值INT_MINint 的最小值UNIT_MAXunsigned int 的最大值LONG_MAXlong 的最大值LONG_MINlong 的最小值LONG_MAXunsigned long 的最大值LLONG_MAXlong long的最大值LLONG_MINlong long的最小值ULLONG_MAXunsigned long long 的最大值// exceed.cpp -- exceeding some integer limits #include <iostream> #define ZERO 0 // makes ZERO symbol for 0 value #include <climits> // defines INT_MAX as largest int value int main() { using namespace std; short sam = SHRT_MAX; // initialize a variable to max value unsigned short sue = sam;// okay if variable sam already defined cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited." << endl << "Add $1 to each account." << endl << "Now "; sam = sam + 1; sue = sue + 1; cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited.\nPoor Sam!" << endl; sam = ZERO; sue = ZERO; cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited." << endl; cout << "Take $1 from each account." << endl << "Now "; sam = sam - 1; sue = sue - 1; cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited." << endl << "Lucky Sue!" << endl; return 0; }
运行结果:
Sam has 32767 dollars and Sue has 32767 dollars deposited. Add $1 to each account. Now Sam has -32768 dollars and Sue has 32768 dollars deposited. Poor Sam! Sam has 0 dollars and Sue has 0 dollars deposited. Take $1 from each account. Now Sam has -1 dollars and Sue has 65535 dollars deposited. Lucky Sue!进制之间的转化
输出结果:
Monsieur cuts a striking figure! chest = 42 (decimal for 42) waist = 2a (hexadecimal for 42) inseam = 52 (octal for 42) 注意hex、oct、dec标记的效力是使cout对未来遭遇的所有整型变量都使用指定的方式输出(直到被下一个标记改变位置),而不仅限于标记之后的一个变量.输出结果:
Put in a char: 9 The ASCII code for 9 is 57cout.put()可以将括号中的数字转化为字符输出。
#include <iostream> using namespace std; int main() { int n = 112; cout.put(n); cout << endl; // cout.put(103); return 0; }输出结果:
p g输出结果:
Operation "HyperHype" is now activated! Enter your agent code:12345678 You entered 12345678... Code verified! Proceed with Plan Z3! 可以基于字符的八进制和十六进制编码使用转义序列。 #include <iostream> using namespace std; int main() { char ch = '\106'; cout << ch << endl; ch = '\046'; cout << ch << endl; ch = '\x74';//不能写成'\0x74' cout << ch << endl; return 0; }输出结果:
F & t输出结果:
1 1 1 1 1 0使用限定符后,ans是一个不可更改的常量。