欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-24 12:56:33
c++自学之路:3.1-简单变量。 #include #include int main(int argc, const char * argv[]) { us...

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

#include 
#include 

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 
#define zero 0;
#include 

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 

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 

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

上一篇:

下一篇: