基本变量类型

C++ has some original variable type, called seven basic variable.
C++有七个基本变量类型
且C++是面向对象的编程语言,可以对基本变量类型进行组合,也可自己构造自己的数据结构。

  1. /*
  2. * Time: 2021-1-15
  3. * Author: Fei Dongxu <Phil616@163.com>
  4. */
  5. #include <iostream>
  6. int main(){
  7. int IntegerType;
  8. bool BooleanType;
  9. char CharacterType;
  10. float FloatingType;
  11. double DoubleFloatingType;
  12. void NullType;
  13. wchar_t WideCharacterType;
  14. //typedef short int wchar_t;
  15. //This is how the wchar_t come from.
  16. return 0;
  17. }

算术类型

整型

字符型和布尔型也是整型

浮点型

修饰符类型

  1. /*
  2. Some of the key words can be complex together.
  3. like the program below:
  4. */
  5. unsigned long int
  6. long long int
  7. unsigned short
  8. /*
  9. To understand the limit size of these types, we can use headfile <limits.h> to study.
  10. */

类型大小

This is a example which runoob.com given to us.

  1. #include<iostream>
  2. #include <limits>
  3. using namespace std;
  4. int main()
  5. {
  6. cout << "type: \t\t" << "************size**************"<< endl;
  7. cout << "bool: \t\t" << "Number of bytes: " << sizeof(bool);
  8. cout << "\tMax:" << (numeric_limits<bool>::max)();
  9. cout << "\t\tMin:" << (numeric_limits<bool>::min)() << endl;
  10. cout << "char: \t\t" << "Number of bytes: " << sizeof(char);
  11. cout << "\tMax:" << (numeric_limits<char>::max)();
  12. cout << "\t\tMin:" << (numeric_limits<char>::min)() << endl;
  13. cout << "signed char: \t" << "Number of bytes: " << sizeof(signed char);
  14. cout << "\tMax:" << (numeric_limits<signed char>::max)();
  15. cout << "\t\tMin:" << (numeric_limits<signed char>::min)() << endl;
  16. cout << "unsigned char: \t" << "Number of bytes: " << sizeof(unsigned char);
  17. cout << "\tMax:" << (numeric_limits<unsigned char>::max)();
  18. cout << "\t\tMin:" << (numeric_limits<unsigned char>::min)() << endl;
  19. cout << "wchar_t: \t" << "Number of bytes: " << sizeof(wchar_t);
  20. cout << "\tMax:" << (numeric_limits<wchar_t>::max)();
  21. cout << "\t\tMin:" << (numeric_limits<wchar_t>::min)() << endl;
  22. cout << "short: \t\t" << "Number of bytes: " << sizeof(short);
  23. cout << "\tMax:" << (numeric_limits<short>::max)();
  24. cout << "\t\tMin:" << (numeric_limits<short>::min)() << endl;
  25. cout << "int: \t\t" << "Number of bytes: " << sizeof(int);
  26. cout << "\tMax:" << (numeric_limits<int>::max)();
  27. cout << "\tMin:" << (numeric_limits<int>::min)() << endl;
  28. cout << "unsigned: \t" << "Number of bytes: " << sizeof(unsigned);
  29. cout << "\tMax:" << (numeric_limits<unsigned>::max)();
  30. cout << "\tMin:" << (numeric_limits<unsigned>::min)() << endl;
  31. cout << "long: \t\t" << "Number of bytes: " << sizeof(long);
  32. cout << "\tMax:" << (numeric_limits<long>::max)();
  33. cout << "\tMin:" << (numeric_limits<long>::min)() << endl;
  34. cout << "unsigned long: \t" << "Number of bytes: " << sizeof(unsigned long);
  35. cout << "\tMax:" << (numeric_limits<unsigned long>::max)();
  36. cout << "\tMin:" << (numeric_limits<unsigned long>::min)() << endl;
  37. cout << "double: \t" << "Number of bytes: " << sizeof(double);
  38. cout << "\tMax:" << (numeric_limits<double>::max)();
  39. cout << "\tMin:" << (numeric_limits<double>::min)() << endl;
  40. cout << "long double: \t" << "Number of bytes: " << sizeof(long double);
  41. cout << "\tMax:" << (numeric_limits<long double>::max)();
  42. cout << "\tMin:" << (numeric_limits<long double>::min)() << endl;
  43. cout << "float: \t\t" << "Number of bytes: " << sizeof(float);
  44. cout << "\tMax:" << (numeric_limits<float>::max)();
  45. cout << "\tMin:" << (numeric_limits<float>::min)() << endl;
  46. cout << "size_t: \t" << "Number of bytes: " << sizeof(size_t);
  47. cout << "\tMax:" << (numeric_limits<size_t>::max)();
  48. cout << "\tMin:" << (numeric_limits<size_t>::min)() << endl;
  49. cout << "string: \t" << "Number of bytes: " << sizeof(string) << endl;
  50. // << "\tMax:" << (numeric_limits<string>::max)() << "\tMin:" << (numeric_limits<string>::min)() << endl;
  51. cout << "type: \t\t" << "************size**************"<< endl;
  52. return 0;
  53. }

它的输出结果是:

  1. type: ************size**************
  2. bool: Number of bytes: 1 Max:1 Min:0
  3. char: Number of bytes: 1 Max: Min:€
  4. signed char: Number of bytes: 1 Max: Min:€
  5. unsigned char: Number of bytes: 1 Max: Min:
  6. wchar_t: Number of bytes: 2 Max:65535 Min:0
  7. short: Number of bytes: 2 Max:32767 Min:-32768
  8. int: Number of bytes: 4 Max:2147483647 Min:-2147483648
  9. unsigned: Number of bytes: 4 Max:4294967295 Min:0
  10. long: Number of bytes: 4 Max:2147483647 Min:-2147483648
  11. unsigned long: Number of bytes: 4 Max:4294967295 Min:0
  12. double: Number of bytes: 8 Max:1.79769e+308 Min:2.22507e-308
  13. long double: Number of bytes: 8 Max:1.79769e+308 Min:2.22507e-308
  14. float: Number of bytes: 4 Max:3.40282e+38 Min:1.17549e-38
  15. size_t: Number of bytes: 4 Max:4294967295 Min:0
  16. string: Number of bytes: 28
  17. type: ************size**************

有一些其他类型的数据在不同的环境下不一致,可用通过循环来看一看他们的最大值和最小值

  1. /*
  2. * Time: 2021-1-15
  3. * Author: Fei Dongxu <Phil616@163.com>
  4. */
  5. #include<iostream>
  6. using namespace std;
  7. typedef short testedType;
  8. //We can change int to any other type.
  9. int main()
  10. {
  11. testedType test = 0;
  12. int max = 0;
  13. int min = 0;
  14. int flag = 1;
  15. while (1){
  16. test++;
  17. if(max < test) max = test;
  18. if(min > test) min = test;
  19. cout << test << " max is " << max << " min is " << min << endl;
  20. }
  21. return 0;
  22. }
  23. //This is a dead loop, we have to stop it in console or debug toolbox.
  24. //while the min and max is no longer change.

类型选择

算术表达式中不要选择char和bool,因为char有时候有符号有时候无符号
只有确认不会涉及负数的时候才使用unsigned类型
优先使用double,float和double的区别不大,但是long double类型不必要

脱离环境行为

在一些不同的机器上运行程序需要考虑环境的问题,编译器和系统环境不同会导致变量的尺寸不一致,需要确定一个在其他编译器或其他环境都不变的变量尺寸才能确保可移植性。

无符号

对于无符号的运算需要参考CSAPP上的实验和算法,日常使用只需要进行测试即可。

字面值变量

字面值指的是1,0,2.3这类常值

转义序列

C++和C都支持一些来源于系统的转义序列

  1. 换行符 \n
  2. 横向制表符 \t
  3. 报警响铃符 \a
  4. 纵向制表符 \v
  5. 退格符 \b
  6. 单引号 \'
  7. 双引号 \"
  8. 反斜线 \\
  9. 问号 \?
  10. 回车符 \r
  11. 进纸符 \f

转义序列也可以进行一些泛化,例如下面这类可以表示一般字符的情况

  1. \7 响铃
  2. \0 空字符
  3. \12 换行符
  4. \40 空格
  5. \115 字母M
  6. \x4d 字母M

指定字面值的类型

如果添加一些前缀和后缀会改变字面值本来的类型
例如

  1. L'a' //wchar_t的宽字符型字面值
  2. u8"Hi!" //utf8字符串,用8位编码一个Unicode字符
  3. 42ULL //Unsigned long long 类型
  4. 1E-3F //科学计数法的单精度float
  5. 3.14159L //long double类型

需要注意long类型的L需要大写,虽然小写符合规则,但是由于1和l的相似性容易失误。

  1. 字符和字符串字面值
  2. 前缀 | 含义 | 类型
  3. u Unicode 16字符 char16_t
  4. U Unicode 32字符 char32_t
  5. L 宽字符 wchar_t
  6. u8 UTF-8(仅字符串) char
  7. 整型字面值
  8. 后缀 | 最小的匹配类型
  9. u/U unsigned
  10. l/L long
  11. ll/LL long long
  12. 浮点型字面值
  13. f/F float
  14. l/L long double

初始化

当我们使用变量时应该进行初始化。

  1. //This is the usual way to initialize variables
  2. int i = 0;
  3. float f = 0;
  4. double d = 0;
  5. char c = '\0';
  6. int *p = NULL;

定义和初始化的顺序是从左到右,那么可以在同一行进行定义和初始化的操作

  1. double pi = 3.14,d = 2*pi*2;
  2. //pi先被定义,然后用于初始化d

列表初始化

C++提供了多种初始化的方式,使用花括号初始值的方式属于列表初始化。
如果列表初始化在一开始就有丢失数据的风险,编译器会报错。

  1. int i = 0;
  2. int i = {0};
  3. int i{0};
  4. int i(0);

如果变量不被初始化,则进行使用,那么可能会产生意想不到的错误或编译器报错和警告。

声明和定义

extern可以进行声明,且可以进行多次声明,但是一旦函数被定义,则不可重定义

变量的作用域

  1. #include <iostream>
  2. using namespace std;
  3. int g_var01 = 10;
  4. int main(){
  5. int var01 = 20;
  6. return 0;
  7. }
  8. void func(){
  9. int var02 = 30;
  10. }
  11. //g_var01 can be used at all functions
  12. //var01 and var02 can only be used at local functions.

常量

100 is a constant data.
“This is a String” is also a constant data.

  1. const double pi = 3.14;
  2. pi = 3.0;//The complier will report an error. caz u cannot change a const int type.