编程的过程中,我们要和各种类型的数据打交道。可以说:编程就是操作数据。下面我们先来介绍整数数据类型int

int是最常用的整数数据类型。

  1. int i; // declare a int variable
  2. int j = 10; // declare and initialize int variable
  3. int k; // declare a int variable
  4. k = 20; // assign a value 赋值操作

:::info Remember to initialize a variable! 一定要记得给声明的变量初始化
否则,语法是没有问题的,不会报错也不会报警,但是结果可能不是你想要得到的 :::

  1. // init.cpp
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int num1; //bad: uninitialized variable
  7. int num2; //bad: uninitialized variable
  8. cout << "num1 = " << num1 << endl;
  9. cout << "num2 = " << num2 << endl;
  10. }
image.png image.png

- Uninitialized variables may have random values.
- The behavior depends on the compiler. Clang (x86_64) and Clang (arm64) in the demo. 如果跨平台,可能会遇到不可复现的错误
- Please initialize variables EXPLICITLY! 一定要初始化!
  • 在C/C++编程中,如果没有对变量进行初始化,该变量的值在不同的编译器、不同的平台下面可能会产生不同的结果。
  • JAVA中如果声明变量不初始化,是会报错的,代码编译不通过。

    How to initialize

    ```cpp int num; num = 10; // do not forget this line

// common methods int num = 10; int num (10); int num {10};

  1. <a name="Me1RH"></a>
  2. ## overflow
  3. ```cpp
  4. // overflow.cpp
  5. #include <iostream>
  6. using namespace std;
  7. int main()
  8. {
  9. int a = 56789;
  10. int b = 56789;
  11. int c = a * b;
  12. cout << "c = " << c << endl;
  13. // unsigned int c1 = a * b; //danger operation
  14. // cout << "c1 = " << c1 << endl;
  15. return 0;
  16. }

The output is a negative number! -1069976775(负数)
程序期望的结果The correct result is 3,224,990,521 ,其16进制表示:0x C0 39 73 39,转化成二进制为:1100 0000 0011 1001 0111 0011 0011 1001,最高位为1,也就是符号位为1。The sign bit is 1!所以程序输出为负数。

Because 56789 is 0xDDD5(16进制表示,1101 1101 1101 0101 二进制表示),1个字符表示4位,第一个字符是D>8,则第一个字符的最高位为1,它是一个满16 位的整数。如果按照32位数来计算,0xDDD5高16位为0,低16位有值。这样的两数相乘,结果必然是满32位的数,最高位为1,也就是符号位为1。The sign bit is 1!所以程序输出为负数。。

signed and unsigned

有符号整数和无符号整数,整数大多情况下是32位的。
对于overflow.cpp的例子,如果想得到正确结果,可以将变量声明成无符号整数。

  1. unsigned int a = 56789;
  2. unsigned int b = 56789;
  3. unsigned int c = a * b;

image.png

  • signed int can be shorten as int. Its range is 2.1 integer-numbers - 图4 if it’s 32-bit. 平时的int类型都是有符号整数。
  • unsigned int: Its range is 2.1 integer-numbers - 图5 if it’s 32-bit. 无符号位,表示正整数。
  • 32 bits for most modern systems, 16 for some old ones. :::info 整数运算遇到overflow的情况,使用无符号整数可以解决,但是在实际应用中,做运算的时候,一定要了解数值的取值范围是多少。注意是否位数不够,溢出(overflow)。很容易出现bug,且难以debug。 ::: 如果32位整数类型还不够用,可以用long int,让整数更长一点;
    如果用不到正常的整数类型位数,可以使用short int,让整数位数少一点。

C and C++ standards do not fix the widths of them.
C/C++ 的标准并没有对int的长度做固定,JAVA中int类型的数据长度固定32位。
image.png

  • Width in bits of different data models
  • sizeof operator can return the width in bytes.

sizeof:It is an operator, not a function!

  1. // size.cpp
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int i = 0;
  7. short s = 0;
  8. cout << "sizeof(int)=" << sizeof(int) << endl;
  9. cout << "sizeof(i)=" << sizeof(i) << endl;
  10. cout << "sizeof(short)=" << sizeof(s) << endl;
  11. cout << "sizeof(long)=" << sizeof(long) << endl;
  12. cout << "sizeof(size_t)=" << sizeof(size_t) << endl;
  13. return 0;
  14. }
  15. // ---- result ---- //
  16. // sizeof(int)=4
  17. // sizeof(i)=4
  18. // sizeof(short)=2
  19. // sizeof(long)=8
  20. // sizeof(size_t)=8

sizeof 不是一个函数,函数的参数必须是变量,而sizeof的参数不是变量,所以是操作符。