Home  >  Article  >  Backend Development  >  Chapter 2 C++: Variables and Basic Types

Chapter 2 C++: Variables and Basic Types

php是最好的语言
php是最好的语言Original
2018-08-06 10:32:371837browse

Introducing the basic knowledge of the language and the standard library

Chapter 2 Variables and Basic Types

Some languages, such as Smalltalk and Python, check the data type when the program is running; In contrast, C is a statically typed language, where type checking occurs at compile time.
- Built-in type: A type defined by the language, such as int.
- Class type: Programmer-defined data type.

Data types tell us what the data means and what operations we can perform on the data.

Basic built-in types

Term chunk (block): The most basic method used to handle storage and make the storage have a structure. The number of bits in a block is generally a power of 2, so that 8, 16, 32, 64, or 128 bits can be processed at a time. Pay attention to distinguish blocks.

The smallest addressable memory block is called "byte"; the basic unit of storage is called "word", which usually consists of several bytes.
- Arithmetic type (arithmetic type) : integer, floating point number, single character, Boolean value
- Integral type : The arithmetic types representing integers, characters and Boolean values ​​are collectively called integral types
- Character type: char (single machine byte: byte) and wchar_t (wide character type, used to extend character sets, such as Chinese characters and Japanese)
- short: half a machine word; int: one machine word; long: two machine words. ps: The word lengths of int and long are usually equal in 32-bit machines. The minimum storage space is 16 bits, 16 bits and 32 bits respectively
- Assignment of integer type: When assigning a value, take the value modulo the number of values ​​of the type.
- Floating point type: single precision, double precision, extended precision (long double)
- Generally speaking, float occupies 1 word (32 bits), double occupies 2 words (64 bits), and long double is represented by 3 or 4 words (96 or 128 bits).
- Usually double is chosen, float has a large loss of accuracy (double can guarantee at least 10 significant digits, float can only guarantee 6 digits), and the calculation cost of double is negligible compared to float.
- The maximum number of 16-bit signed is 32767, and the maximum number of unsigned is 65535.
- Empty type (void): Usually used as the return type of functions without return values

Type conversion

unsigned char c = -1;     //假设char占8比特,c的值为255signed char c2 = 256;     //假设char占8比特,c2的值是未定义的
  • When we assign When an unsigned type is given a value outside the range it represents, the result is the remainder of the initial value modulo the total number of values ​​represented by the unsigned type.

  • When we assign a value to a signed type that is outside the range it represents, the result is undefined (undefined). At this point, the program may continue to work, may crash, or may generate garbage data.

  • Signed and unsigned types cannot be mixed. If both types are present in an expression, signed numbers are automatically converted to unsigned numbers.

Literal constant (literal constant)

  • It is called a literal value because it can only be called by its value. A constant is because its value cannot be modified.

  • Only built-in types have literal values, there are no literal values ​​for class types, and therefore there are no literal values ​​for standard library types.

  • Use decimal, octal (starting with 0) or hexadecimal (starting with 0x or 0X)

//20的三种表示:20      /*十进制*/024     /*八进制*/0x14    /*十六进制*/
  1. Integer literal value: The literal constant type defaults to int or long type. By adding a suffix, the literal value can be forced to be converted into long, unsinged, or unsigned long. The suffix is ​​L, U, UL, or LU (lowercase is also acceptable. The use of l is not recommended, and it is easy to be confused with 1).

  2. Floating point literal value: expressed in decimal or scientific notation (use e or E). The default double type is, add F or f to indicate single precision, add L or l to indicate extended precision.

  3. Boolean literals: true and false.

  4. Character literal value:

    ##NameWritingLine break\n##Horizontal tabVertical tab characterBackspace characterCarriage returnQuestion markDouble quotes
    • \ooo:这里的ooo表示三个八进制数字,这三个数字表示字符的数字值。如’\0’通常表示“空字符(null character)”。

    • 也可以用十六进制转义字符来定义:\xddd。一个反斜线、一个x和一个或多个十六进制数组成。

    • 通用转义字符:

    • 可打印字符通常用一对单引号定义,如‘a’;在前面加L就能得到wchar_t类型的宽字符字面值,如L‘a’。

    • 不可打印字符和特殊字符都用转义字符书写,转义字符以反斜线开始。

  5. 字符串字面值

    • 双引号括起来的0个或多个字符。

    • 为了兼容C语言,C++所有的字符串字面值都由编译器自动在末尾添加一个空字符(‘\0’),因此,其实际长度要比它的内容多1。

    • 宽字符字面值:在字符串前面加L,如L“asdff”。

    • 多行字面值:两个字符串字面值位置紧邻且仅由空格、缩进和换行符分割,则它们实际上是一个整体。

    • 不依赖未定义行为和机器相关行为,否则这样的程序时不可移植的(nonportable)。

  6. 指针字面值:nullptr

  7. 变量

    变量提供了程序可操作的有名字的存储区
    - 左值右值
      - 左值(lvalue):变量的地址,或者是一个代表“ 对象在内存中的位置”的表达式。
      - 右值(rvalue):变量的值

    变量名出现在赋值运算符的左边,就是一个左值;而出现在赋值运算符右边的变量名或字面常量就是一个右值。
    
        如:
        val1=val2/8
        这里的val1是个左值,而val2和8都是右值。

    - 对象:内存中具有类型的区域
    - 变量名:即变量的标识符(identifier)。
       1. 由字母、数字和下划线组成.
       2. 变量名必须以字母或下划线开头,并且区分大小写。(函数体外的变量名不能以下划线开头)
       3. C++关键字不能用做标识符
       4. 不能包含两个连续的下划线
       5. 不能以下划线开头后紧跟一个大写字母

    • 定义对象(如:int a;)

      每个定义都是以类型说明符(type specifier)开始的(如:int)

      int ival(1024);//direct-initialization,直接初始化int ival = 1024;//copy-initialization,复制初始化//注:直接初始化语法更灵活,且效率更高。
      • 列表初始化(list initialization)(C++11新特性)

        int ival{1024};int ival = {1024};long double ld = 3.1415926536;int a{ld}, b = {ld};    //错误:转换未执行,因为存在丢失信息的风险int c(ld), d = ld;      //正确:转换执行,且丢失部分值
      • 初始化&赋值:初始化不是赋值。

      • 默认初始化(default initialized):如果内置类型的变量未被显示初始化,它的值由定义的位置决定。定义于任何函数体之外的变量被初始化为0,内部的将不被初始化。

      • 建议初始化每个内置型变量,以保证程序安全。

      • 初始化

    • 变量的声明

      extern int i;   //声明i而非定义iint j;          //定义j
      • 变量只能被定义一次,但能被多次声明。

      • 如果要在多个文件中使用同一个变量,就必须将声明和定义分离。变量的定义必须出现且只能处在在一个文件中,而其他用到该变量的文件必须对其进行声明,却决不能重复定义。

      • 如果想声明一个变量而非定义它,在变量名前加关键字extern,且不要显示地初始化变量:

    • 变量名的作用域(scope):以花括号分隔

      全局作用域(global scope)

      块作用域(block scope)

    复合类型(compound type)

    • 引用(reference),此处指左值引用(lvalue reference)。

      int ival = 1024;int &refVal = ival; //refVal指向ival(是ival的另一个名字)int &refVal;       //报错,引用必须被初始化
      • 引用不是对象,只是已经存在的对象的另一个名字。程序将引用和他的初始值绑定(bind)在一起,而不是直接将初始值拷贝给引用。

    • 指针(pointer)

      int i = 42;     
      int &r = i;     //&紧随类名出现,因此是声明的一部分,r是一个引用int *p;         //*紧随类名出现,因此是声明的一部分,p是一个指针p = &i;         //&出现在表达式中,是一个取地址符*p = i;         //*出现在表达式中,是一个解引用符int &r2 = *p;
      • 指针是对象,允许赋值和拷贝,且在指针的生命周期内它可以先后指向不同的几个对象。

      • 指针无需在定义时赋值。

      • 指针类型和它所指向的对象的类型必须匹配。

        int *ip1, *ip2;int val = 4;int *p = &val;
      • 如果指针指向了一个对象,则允许用解引用符(操作符*)来访问该对象:

        int ival - 42;int *p = &ival;cout << *p;     //由符号*得到指针p所指的对象,输出42*p = 0;         //由符号*得到指针p所指的对象,即可经由p为变量ival赋值cout << *p;     //输出0
      • 符号的含义由上下文决定

    • 空指针

    int *p1 = nullptr;int *p2 = 0;int *p3 = NULL;     //需要首先#include cstdlib
    • 建议初始化所有指针

    • 面对一条比较复杂的指针或引用的声明语句时,从右向左阅读有助于弄清它的真实含义。

    const限定符

    定义常量。
    - 指针和const
       - 顶层const(top-level const):表示指针本身是个常量
       - 底层const(low-level condt):表示指针所指的对象是个常量

    • 指向常量的指针(pointer to const)

      const double pi = 3.14;double *ptr = &pi;          //错误!const double *cptr = &pi;   //正确*cptr = 42;                 //错误!cptr指向常量,不能向常量赋值double dval = 3.14;
      cptr = &dval;               //正确,但不能通过cptr改变dval的值,因为cptr以为自己指向的是常量
    • 常量指针(const pointer)

      int errNum = 0;int *const curErr = &errBum;    //curErr将一直指向errNumconst double pi = 3.14;const double *const pip = &pi;  //pip是一个指向常量对象的常量指针
    • constexpr和常量表达式

      • C++11新标准:将变量声明为constexpr类型以便由编译器来验证变量的值是否是一个常量表达式。(用const,有些常量的具体值直到运行时才能获取)

      • 在constexpr声明中如果定义了一个指针,限定符仅对指针有效,与指针所知的对象无关。也就是说它把所定义的对象置为顶层const。

        const int *p = nullptr;     //p是一个指向常量的普通指针constexpr int *q= nullptr;  //q是一个常量指针constexpr int i = 42;constexpr const *p = &i;

    处理类型

    • 类型别名(type alias)

      • typedef
        <br>typedef double wages;   //wages是double的同义词 <br>typedef wages base, *p; //base是double的同义词,p是double*的同义词 <br>

      • 别名声明(alias declaration)
        <br>using S1 = Sales_item;  //S1是Sales_item的同义词 <br>

    • auto类型说明符

      • 让编译器替代我们去分析表达式所属的类型。

      • auto一般会忽略顶层const
        <br>const int i =1;     //i是整型常量 <br>auto b = i;         //b是一个整数 <br>const auto c = i;   //c是整型常量 <br>

    • decltype类型指示符

      const int ci = 0, &cj = ci; 
      decltype(ci) x = 0;         //x的类型是const intdecltype(cj) y = x;         //y的类型是const int&,y绑定到变量xdecltype(cj) z;             //错误:z是一个引用,必须初始化
      int i = 42, *p = &i, &r = i;
      decltype(r + 0) b;  //正确:加法的结果是int,因此b是一个未初始化的intdecltype(*p) c;     //错误:c是int&,必须初始化
      • decltype((v))(注意双层括号)的结果永远是引用。

      • decltype处理顶层const和引用的方式与auto有些许不同。如果decltype使用的表达式是一个变量,则decltype返回该变量的类型(包括顶层const和引用在内)。

    自定义数据结构

    • 头文件通常包含哪些只能被定义一次的实体,如类、const和constexpr变量等。

    • 预处理器(preprocessor):如#include,当预处理器看到#include标记时就会用指定的头文件内容替代#include。

    • 头文件保护符(header guard):有效防止重复包含发生

      • 一般把预处理变量的名字全部大写
        <br>#ifdef      //当且仅当变量已定义时为真 <br>#ifndef     //当且仅当变量未定义是为真 <br>#define     //把一个名字设定为预处理变量 <br>#endif      //与#ifdef和#ifndef匹配,执行它们的后续操作知道#endif指令止 <br>

    #ifndef SALES_DATA_H#define SALES_DATA_H#include <string>struct Sales_data {    ... //此处省略
    };#endif

    参考:C++Primer第五版

    介绍语言的基本知识和标准库

    相关文章:

    第一章C++:函数返回值、GNU编译器命令

    第三章C++:字符串string、向量vector和数组

    \t
    \v
    \b
    \r
    \?
    \"

The above is the detailed content of Chapter 2 C++: Variables and Basic Types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn