C++自学之路:3.1-简单变量

来源:互联网 发布:淘宝联盟身份验证 编辑:程序博客网 时间:2024/06/05 20:25
#include <iostream>#include <climits>int main(int argc, const char * argv[]) {        using namespace std;        int n_int = INT_MAX;    short n_short = SHRT_MAX;    long n_long = LONG_MAX;    long long n_llong = LLONG_MAX;        cout << "int is " << sizeof(n_int) << " bytes." << endl;    cout << "short is " << sizeof(n_short) << " btyes." << endl;    cout << "long is " << sizeof(n_long) << " bytes." << endl;    cout << "long long " << sizeof(n_llong) << " bytes." << endl;    cout << endl;        cout << "Maxium values:" << endl;    cout << "int:" << n_int << endl;    cout << "short:" << n_short << endl;    cout << "long:" << n_long << endl;    cout << "long long:" << n_llong << endl;        cout << "Minium int values:" << endl;    cout << "Bits per byte = " << CHAR_BIT << endl;         return 0;}
sizeof 指出,在使用8位字节的系统中,int长度为4个字节,可以对类名或者变量名使用sizeof运算符,类名需要放在括号中,变量名括号可选。
cout << "short is " << sizeof(int) << " btyes." << endl;

cout << "short is " << sizeof n_short << " btyes." << endl;

#include <iostream>#define ZERO 0;#include <climits>int main(int argc, const char * argv[]) {    using namespace std;        short sam = SHRT_MAX;    unsigned short sue = sam;        cout << "Sam has " << sam << " dollars and Sue has " << sue;    cout << " dollars deposited." << endl         << "Add $1 to each accout." << 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 accout." << 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 accout.
     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 accout.
     Now Sam has -1 dollars and Sue has 65535 dollars deposited.
     Lucky Sue!
     
     short 变量sam 和 unsigned short 变量 sue,分别设置最大值和最小值。short变量从最大值加1,取值从32768变成—32769。unsigned short变量从最大值加1并没有什么问题。short变量从最大值减1,没什么问题。unsigned short变量从最大值加1,从0变为65535。
     如果超越了范围,其值将从范围另一端取值。
     
     无符号类型:
     unsigned short change;
     unsigned int rovert;
     unsigned quarterback;//unsigned 本身是 unsigned int 缩写。
     unsigned long gone;
     unsigned long long lang_lang;
     //无符号类型的正值更大。
     
     选择整型类型:
     1.int 被设置为对目标计算机而言最为自然的长度。
     2.如果变量表示的值不可能为负,如文档中的字数,则可以使用无符号类型,这样可以表示更大的值。

#include <iostream>using namespace std;int main(int argc, const char * arvg[]) {        using namespace std;    int chest = 42;//10进制    int waist = 0x42;//16进制    int inseam = 042;//8进制        cout << "Monsieur cuts a striking figure!\n";    cout << "chest = " << chest << " (42 in decimal)\n";    cout << "waist = " << waist << " (0x42 in hex)\n";    cout << "inseam = " << inseam << " (042 in octal)\n" << endl;        //  1.默认情况下cout以十进制显示。        return 0;}
#include <iostream>using namespace std;int main(int argc, const char * arvg[]) {    using namespace std;    int chest = 42;    int waist = 42;    int inseam = 42;        cout << "Monsieur cuts a striking figure!" << endl;    cout << "chest = " << chest << " (decimal for 42)" << endl;    cout << hex;    cout << "waist = " << waist << " (hexadecimal for 42)" << endl;    cout << oct;    cout << "inseam = " << inseam << " (octal for 42)" << endl;         //  hex将值转换成16进制显示。oct将值转换成8进制显示。dec将值转换为10进制显示。     return 0;}
C++确定常量类型
 1.除非有理由将值储存为其他类型,否则C++将整型常量储存为int类型。
 2.整数后面l或者L表示long类型,u或者U表示unsigned int 类型,ul表示 unsigned long 类型,ull或者uLL表示unsigned long long类型。
 3.C++中,对于10进制数值的规则。(int为16位的机器中)20000表示为int类型,40000表示为long类型,3000000000表示为long long 类型。



原创粉丝点击