//============================================================================ // Name : HelloWorld.cpp // Author : kinglo // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #define ZERO 0 /* 沿用c语言的常量定义 c++可以作用const*/ #include <limits> /* 导入限定文件 */ using namespace std; int main() { cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! /** *数据类型 *在不同系统中,c++相同的数据类型占用内存可能不同,故在climits(limits.h)对各个数据类型的取值泛围 做了限定 * */ int n_int = INT_MAX; /* INT_MAX int型的最大值 */ short n_short = SHRT_MAX; /* 短整型最大值 */ long n_long = LONG_MAX; /* 长整型最大值 */ /** * CHAR_BIT char的位数 * CHAR_MAX char的最大值 * CHAR_MIN char的最小值 * SCHAR_MAX signed char 的最大值 * SCHAR_MIN signed char 的最小值 * UCHAR_MAX unsigned char 的最大值 * SHRT_MAX short 的最大值 * SHRT_MIN short 的最小值 * USHRT_MAX unsigned short 的最大值 * INT_MAX int 的最大值 * INT_MIN int 的最小值 * UINT_MAX unsigned int 的最大值 * LONG_MAX long 的最大值 * LONG_MIN long 的最小值 * ULONG_MAX unsigned long 的最大值 */ cout << "int is " << sizeof(int) << " bytes ." <<endl; cout << "short is " << sizeof n_short <<" bytes ." <<endl; cout << "short is " << sizeof(short) <<" bytes ." <<endl; cout << "long is " << sizeof n_long <<" bytes ."<<endl; cout << "long is " << sizeof(long) <<" bytes ."<<endl; cout << "Maximum values: " << endl; cout << "int: " << n_int <<endl; cout << "short: "<< n_short <<endl; cout << "long : "<< n_long << endl; cout << "Minimum int value = " << INT_MIN << endl; cout << "Bits per byte = "<< CHAR_BIT << endl; /** *程序超越数据类型限定的后果 * */ cout << "*******************************************/n"; short sam = SHRT_MAX; unsigned short sue = sam; cout << "Sam has " << sam << " dollars and Sue has "<< sue; cout << "dollars deposited." << endl; cout << "And $1 from each account." << endl <<"Now "; cout << sam + 1; cout << 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; cout << "*******************************************/n"; /** * 数据在程序的表达 * */ int cheat = 42; int waist = 42; int inseam = 42; cout << " Monsieur cuts a striking figure " <<endl; cout << " chest = " << cheat << " (decimal)"<<endl; cout << hex; //更改显示整数的方式为十六进制 cout << " waist = " << waist << "hexadecimal"<<endl; cout << oct; //更改显示整数的方式的八进制 cout << " inseam = " << inseam << " (octal)" <<endl; return 0; }