1.int(整型数据):
1.1 进制的表示:十进制,八进制,16进制,二进制
int number1 = 15; // Decimal int number2 = 017; // Octal int number3 = 0x0F; // Hexadecimal int number4 = 0b00001111; // Binary
上面几种表示方式都表示15这个数字,用cout输出得到相同的结果
1.2 声明和初始化:int类型占据4个字节(32位)
声明并且初始化一个整数类型可以使用大括号,括号和=。
利用大括号{}进行声明和初始化:
int elephant_count; //Variable may contain random garbage value . WARNING int lion_count{};//Initializes to zero int dog_count {10}; //Initializes to 10 int domesticated_animals { dog_count + cat_count }; int narrowing_conversion {2.9};//Compiler error
利用括号()进行声明和初始化:
int test(); //error
int orange_count(10); int fruit_count (apple_count + orange_count); int narrowing_conversion_functional (2.9); //Information lost.
利用=进行初始化:
int truck_count = 7; int vehicle_count = bike_count +truck_count; int narrowing_conversion_assignment = 2.9; //information lost
通过对比可以发现,{}来初始化整型数据是最安全的,对于没有赋初值的整型数据会自动初始化为0,对于赋值为浮点数的数据类型会报编译器错误。
1.3 long/short, signed/unsigned 标识符
unsigned int range 0~2^n-1 4 bytes
signed int range -2^(n-1)~2^(n-1)-1 4 bytes
//unsigned int value4{-5}; // Compiler error. // short 关键字占据2 Bytes short short_var {-32768} ; // 2 Bytes short int short_int {455} ; // signed short signed_short {122}; // signed short int signed_short_int {-456}; // unsigned short int unsigned_short_int {456}; // 没有short或者long关键字都占据4 Bytes int int_var {55} ; // 4 bytes signed signed_var {66};// signed int signed_int {77};// unsigned int unsigned_int{77}; // long关键字一般占据4 Bytes long long_var {88}; // 4 OR 8 Bytes long int long_int {33}; signed long signed_long {44}; signed long int signed_long_int {44}; unsigned long int unsigned_long_int{44}; // long long关键字占据8 Bytes long long long_long {888};// 8 Bytes long long int long_long_int {999}; signed long long signed_long_long {444}; signed long long int signed_long_long_int{1234}; unsigned long long int unsigned_long_long_int{1234};
标签:count,short,C++,int,数据类型,unsigned,signed,long,整型 From: https://www.cnblogs.com/zspaiprogram/p/17891335.html