Introduction to basic knowledge and standard library of language
Chapter II variables and basic types
Some languages, such as Smalltalk and Python, check the data type while the program is running, whereas C + + is a static data type language whose type checking occurs at compile time.
-Built-in type (built-in type): a type defined by the language, such as Int.
-Class type: The programmer's Custom data type.
+ + data type tell me the meaning of the data and the operations we can perform on the data. ++
Basic built-in types
The term chunk (block): The most basic way to handle storage and make storage have a structure. The number of bits in a block is typically a power of 2, so that you can process 8, 16, 32, 64, or 128 bits at a time. Take care to differentiate the block.
The minimum addressable block of memory is called "byte", and the stored base unit is called word (word), typically consisting of several bytes. ++
- arithmetic type (arithmetic type): integer, floating-point number, single character, Boolean
- integer type (integral type): An arithmetic type that represents integers, characters, and booleans is called integral type
-Character type: char (Single machine Byte: Byte) and wchar_t (wide character type for extended character sets, such as kanji and Japanese)
-Short: half machine word length; int: a machine length; long: two machine length. PS: In a 32-bit machine, the word length of int and long is usually equal. 16-bit, 16-bit, and 32-bit minimum storage space
-the assignment of an integral type: When the value is assigned, it evaluates to the value of the value for that type.
- floating point : Single precision, double precision, extended precision (long double)
-Generally, float accounts for 1 characters (32 bits), double is 2 characters (64 bits), and long double is expressed as 3 or 4 characters (96 or 128 bits).
-The precision loss of the double,float is usually high (double can guarantee a minimum of 10 digits, float can only be guaranteed 6 bits), and the calculation cost of double is negligible relative to float.
-16-bit signed maximum 32767,unsigned maximum number 65535.
- null type (void): Typically used as a return type without a return value function
Type conversions
unsigned char c =-1; Assuming Char is 8 bits, the value of C is 255signed char c2 = zero; Assuming that Char occupies 8 bits, the value of C2 is undefined
-
When we assign an unsigned type to a value that exceeds the range it represents, the result is the remainder of the initial value that represents the total number of numeric values for the unsigned type.
-
When we assign a signed type to a value that exceeds its range, the result is undefined (undefined). At this point, the program may continue to work, may crash, or may generate junk data.
-
You can not mix signed and unsigned types. If both types are present in an expression, the signed number is automatically converted to the unsigned number.
Literal constants (literal constant)
-
It is called a literal because it can only be called by its value, which is called a constant because its value cannot be modified.
-
Only the built-in type has literals, there is no literal value for the class type, and therefore there is no literal value for the standard library type.
-
Use decimal, octal (beginning with 0) or hexadecimal (beginning with 0x or 0X)
20 of three representations: */ * Decimal */024/* octal */0x14/ * hex */
-
-
integer literals: Literal constants type default to int or long. By adding suffixes, you can force the literal value to be converted to long, unsinged, unsigned long, with the suffix L, U, UL, or LU (lowercase also.) The use of L is not advocated, easy to confuse with 1).
-
-
floating-point literals: decimal or scientific notation (with E or e). The default double type, plus F or f for single precision, plus L or L for extended precision.
-
-
Boolean literals : True and False.
-
-
character Literals:
name |
Writing |
Line break |
\ n |
Horizontal tab |
\ t |
Portrait tab |
\v |
Backspace |
\b |
Carriage return character |
\ r |
Question number |
\? |
Double quotes |
\” |
-
\ooo: ooo here represents three octal digits, and these three numbers represent the numeric values of the characters. such as ' character ' usually means "null character (NULL)".
-
You can also use hexadecimal escape characters to define: \XDDD. A backslash, an X, and one or more hexadecimal digits.
-
Printable word wildcard commonly used a pair of single quotation marks, such as ' a ', in front plus l can get wchar_t type of wide character literals, such as L ' a '.
-
Non-printable characters and special characters are written in escape characters, and escape characters begin with a backslash.
-
-
string Literals:
-
0 or more characters enclosed in double quotation marks.
-
To be compatible with C, all string literals in C + + are automatically added by the compiler to the end of a null character ('% '), so its actual length is 1 more than its content.
-
Wide character literals: precede the string with L, such as L "ASDFF".
-
Multiline literals: Two string literals are positioned immediately and only separated by spaces, indents, and line breaks, they are actually a whole.
-
Do not rely on undefined behavior and machine-related behavior, otherwise such programs are not portable (nonportable).
-
-
Pointer literal: nullptr
Variable
Variable provides a named store where the program can manipulate
- left and right values
-Lvalue (Lvalue): The address of a variable, or an expression that represents the location of an object in memory.
-Right value (rvalue): The value of the variable
The variable name appears to the left of the assignment operator, which is an lvalue, and the variable name or literal constant that appears to the right of the assignment operator is a right value. such as: VAL1=VAL2/8 Here The Val1 is an lvalue, and val2 and 8 are right values.
- Object : An area of type in memory
- variable name : That is, the identifier of the variable (identifier).
1. Consists of letters, numbers, and underscores.
2. Variable names must begin with a letter or underscore, and are case-sensitive. (the variable name outside the function body cannot begin with an underscore)
3. C + + keywords cannot be used as identifiers
4. Cannot contain two consecutive underscores
5. Cannot start with an uppercase letter immediately after the underscore
-
-
define objects (e.g. int A;)
Each definition begins with the type specifier (specifier) (for example: int)
int ival (1024x768);//direct-initialization, direct initialization int ival = 1024;//copy-initialization, copy initialization//NOTE: Direct initialization syntax is more flexible and more efficient.
-
listing initialization (list initialization) (C++11 new Features)
int Ival{1024};int ival = {1024};long double ld = 3.1415926536;int A{ld}, B = {ld}; Error: The conversion was not performed because there is a risk of loss of information int C (LD), d = ld; Correct: conversion execution, and loss of partial value
-
Initialization & Assignment: Initialization is not a value assignment.
-
Default initialized: If a variable of a built-in type is not displayed initialized, its value is determined by the location defined. Variables defined outside of any function body are initialized to 0, and the internal ones are not initialized.
-
It is recommended to initialize each built-in variable to ensure program security.
-
Initialization
-
-
Declaration of a variable
extern int i; Declaration I rather than definition iint J; Definition J
-
Variables can only be defined once, but are declared more than once.
-
If you want to use the same variable in more than one file, you must detach the declaration and definition. The definition of a variable must appear and be in only one file, while other files that use the variable must be declared, but cannot be defined repeatedly.
-
If you want to declare a variable instead of defining it, add the keyword extern before the variable name and do not show the Initialize variable:
-
-
Variable name scope (scope): delimited by curly braces
Global scope (Globals scope)
Chunk scope (block scope)
Composite type (compound type)
-
-
reference (reference), here refers to an lvalue reference (Lvalue reference).
int ival = 1024;int &refval = ival; Refval points to Ival (another name for ival) int &refVal; //error, reference must be initialized
-
A reference is not an object, just another name for an object that already exists. Instead of copying the initial value directly to the reference, the program binds the reference to his initial value (BIND).
-
-
pointer (pointer)
int i =; int &r = i; & immediately appears as a class name, so it is part of the declaration, and R is a reference to int *p; * Immediately after the class name appears, so is part of the Declaration, p is a pointer p = &i; & appears in the expression, is a fetch address character *p = i; * appears in the expression, is a dereference int &r2 = *p;
-
Pointers are objects that allow assignment and copying, and in the lifetime of the pointer it can point to several different objects successively.
-
Pointers do not need to be assigned values when defined.
-
The type of the pointer type and the object it points to must match.
int *ip1, *ip2;int val = 4;int *p = &val;
-
If the pointer points to an object, it is allowed to access the object using the dereference (operator *):
int Ival-42;int *p = &ival;cout << *p; By the symbol * Gets the object referred to by the pointer p, output 42*p = 0; By the symbol * to get the object referred to by the pointer p, the variable ival can be assigned cout << *p by P; Output 0
-
The meaning of a symbol is determined by context
-
-
Null pointer
int *P1 = Nullptr;int *p2 = 0;int *p3 = NULL; Need first # include Cstdlib
Const qualifier
Defines constants.
- pointers and const
-Top-level const (top-level const): Indicates that the pointer itself is a constant
-Underlying const (low-level Condt): Indicates that the pointer refers to an object that is a constant
-
Pointer to constant (pointer to const)
Const double PI = 3.14;double *ptr = π Error! Const double *CPTR = π correct *cptr =; Error! Cptr points to constants, cannot assign a value to a constant double dval = 3.14;cptr = &dval; Correct, but cannot change the value of Dval by Cptr, because Cptr thinks he is pointing to a constant
-
Constant pointer (const pointer)
int errnum = 0;int *const curerr = &errBum; Curerr will always point to errnumconst double pi = 3.14;const double *const pip = π PIP is a constant pointer to a constant object
Processing type
-
-
Types alias (type aliases)
-
typedef
Typedef double wages; //wages is a synonym for double
Typedef wages base, *p; //base is a synonym for double, p is a synonym for double*
-
alias statement (aliases Declaration)
using S1 = Sales_item; //S1 is a synonym for Sales_item
-
-
Auto type specifier
-
Let the compiler replace us to parse the type that the expression belongs to.
-
Auto generally ignores top-level const
Const int i =1; //i is an integer constant
Auto b = i; //b is an integer
Const auto c = i; //c is an integer constant
-
-
Decltype Type Indicator
Const int ci = 0, &cj = ci;
Decltype(ci) x = 0; //x type is const intdecltype(cj) y = x; //y type is const int&, y is bound to variable xdecltype(cj) z; //error: z is a Reference, must be initialized
Int i = 42, *p = &i, &r = i;
Decltype(r + 0) b; //correct: the result of the addition is int, so b is an uninitialized intdecltype(*p) c; //error: c is int&, must be initialized
-
Decltype ((v)) (note double brackets) The result is always a reference.
-
Decltype handles top-level const and references in a slightly different way from auto. If the expression used by Decltype is a variable, DECLTYPE returns the type of the variable, including the top-level const and the reference.
Custom Data Structures
-
A header file typically contains entities that can only be defined once, such as classes, const, and constexpr variables.
-
Preprocessor (Preprocessor): such as # include, when the preprocessor sees the # include tag, replaces # include with the specified header file content.
-
Header File Protector (header Guard): Effectively prevents duplicate inclusions from occurring
-
The name of the preprocessing variable is generally capitalized
#ifdef //true if and only if the variable is already defined
#ifndef //If and only if the variable is undefined is true
#define //Set a name as a preprocessor variable
#endif //match #ifdef and #ifndef, perform their subsequent operations to know the #endif command
#ifndef sales_data_h#define sales_data_h#include <string>struct sales_data { ...//omitted here}; #endif
Reference: C++primer Fifth Edition
Introduction to basic knowledge and standard library of language
Related articles:
Chapter One C + +: function return value, GNU compiler command
Chapter III C + +: string strings, vector vectors and arrays