C++ primer plus plus 第二章学习:变量

//c++第二章1.1-3.2

//1)变量名的命名

//c++名称的长度没有限制,所有字符都有意义,可以用下划线或者大写字母隔开eeg:my_book_list  or  myBookList

//_xx,__xx,_X,被保留给实现使用

//2)数据类型

//【整型】基本整型10种:char -> short -> int -> long -> long long (画unsinged ,有符号)

//short至少16位,int至少与short一样长,long至少32位,long long h至少64位

//CHAR_BIT,INT_MAX等表示符号常量被定义在climits文件中

//最好将变量的声明和赋值分开

//usinged是usinged int的缩写

#include <iostream>

扫描二维码关注公众号,回复: 5889371 查看本文章

#include <climits>

int main()

{

    // insert code here...

    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

    cout << "int is " << sizeof(int) << "bytes. " << endl;//对类型名使用sizeof 使用运算符,必须括号

    cout << "short is "<< sizeof(n_short) << "bytes. "<< endl;//对变量名使用sizeof 预算符

    cout << "long is " << sizeof(n_long) << "bytes. "<< endl;

    cout << "long long  is "<< sizeof n_llong<< "bytes. "<< endl;////对变量名使用sizeof 预算符,括号可有可无

    cout << endl;

    

    cout << "Maximum value: "<<endl;

    cout << "int: "<<n_int <<endl;

    cout << "short: "<< n_short << endl;

    cout << "long: "<< n_long << endl;

    cout <<"long long : "<<n_llong <<endl;

    cout <<"minimum value= "<< INT_MIN << endl;

    cout <<"Bits per byte = "<<CHAR_BIT <<endl;

    cout <<endl;

    

    int emus{7};

    int rheas = {12};

    int test = {};

    cout <<"emus = " << emus <<endl;

    cout << "rheas = "<< rheas <<endl;

    cout << "test = "<< test <<endl;

    std::cout << "Hello, World!\n";

    return 0;

}

实验结果:

int is 4bytes.

short is 2bytes.

long is 8bytes.

long long  is 8bytes.

Maximum value:

int: 2147483647

short: 32767

long: 9223372036854775807

long long : 9223372036854775807

minimum value= -2147483648

Bits per byte = 8

emus = 7

rheas = 12

test = 0

Hello, World!

Program ended with exit code: 0

猜你喜欢

转载自blog.csdn.net/zjguilai/article/details/89199863