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

#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 类型。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值