C ++ primer Study Notes chapter 2 (2/18) variables and Basic Types

Source: Internet
Author: User

 


Integer:

Integer short, Int, long can be signed or not signed. It is signed by default.

 

Signed integer:

Indicates positive, 0, and negative. To reflect the symbols, one of them is the symbol bit.

 

Unsigned integer:

Indicates positive and 0.

 

Cross-border assignment:

Most compilers work like this.

When the compiler assigns a value to an unsigned integer, a negative number is not allowed. However, if a negative value is assigned to an unsigned integer, the compiler modulo the negative value. For example, if the 8-bit unsigned char is assigned a value of-1, the result is 255. Because (-1) mod (256) = 255. Similarly, if you assign a positive number out of the value range to an unsigned integer, the modulo operation is also performed. For example, if 336 is assigned an unsigned char value, the result is (336) mod (256) = 80.

When the compiler assigns a value to a signed integer, it takes a modulo similar to an unsigned integer.

 

Floating point type:

It can be divided into single-precision floating point float, double-precision floating point, Extended Floating Point long
Double. ,

 

Character Type:

Although the string type is an integer, it is usually used to store characters.

 

Boolean:

Boolean values and values that can be granted are two concepts.

Boolean values are true and false, and can be assigned a value of any arithmetic type. The value 0 is false, and any non-0 value indicates true.

 

Literal Value constant:

Used as the right value of the operand, that is, the value assigned to the object. Such as 1 and 'A.

The face value of an integer can be expressed in three hexadecimal notation, for example, decimal: 20.
Gossip:
024
Hexadecimal: 0x14

The default value of an integer is Int or long. You can perform type conversion between integer types and add U or L to the value. Such as 2048ul, 6lu, 1l

The floating point nominal value can be expressed in decimal or scientific and technological way.

The float value is double by default. You can convert data types between floating point types. Adding F to the value indicates the single precision, and adding L to the value indicates the extended precision.

For example,. 001f = 1e-3f, 12.345l = 1.2345e1l

True and false are Boolean literal values.

The printable character nominal value is defined by a pair of single quotes. For example, 'A ',
'123 '. Add L before the literal value to obtain the literal value of a wide character. For example, l 'A' and l '123 '.

 

String Literal Value:

Some literal values have basic built-in types, while others do not, such as string literal values. It is a string of constant characters. To be compatible with the C language, the compiler automatically adds an empty character to the end of all character strings. For example, the string literal value "A" contains the letter A and two empty characters.

 

Multiline literal value:

Adding a backslash at the end of a row can treat this row and the next row as the same row.

For example, cout <"a happy/


Sunny/


Day "<Endl;

Equal to cout <"a happy
Sunny day "<Endl;

 

Escape characters: unprintable characters

Variable:

A variable provides a namespace that can be operated by a program. A variable has a specific type, which determines the memory size and layout of the variable, the value range of the value that can be stored in the memory and the operation set that can be applied to the variable. They can be called variables or objects. The variable is the left value.

 

Variable name:

The identifier of the variable. It may consist of letters, numbers, or underscores. For example, studentnumber_class1

 

Variable definition:

You must specify the type and identifier of the variable to provide the initial value for the variable.

 

Variable initialization:

It can be a direct initialization (such as int ival (1024) or a copy initialization (such as int ival = 1024 ). For built-in types, replication initialization and direct initialization are almost identical. For class objects, some initialization can only be completed through direct initialization.

Initialization is not a value assignment. Initialization refers to creating a variable and assigning it an initial value. A value assignment is used to erase the current value of an object and replace it with a new value.

 

Variable initialization rules:

If it is a built-in type variable, all definitions outside the function body (including the main function) are initialized to 0, and those defined in the function body are not automatically initialized. Uninitialized data may cause danger. Therefore, we recommend that you initialize each built-in object. If it is a class type, some classes provide the default constructor. In this case, the constructor is not provided for display initialization.

 

Variable declaration:

Extern keyword. A program is usually composed of multiple files. The Declaration indicates that the variable is defined elsewhere in the program. Variables can be defined only once, but can be declared multiple times.

 

Scope: defined in curly brackets. The scope of a name is to know the scope of its declaration from its declaration point.

 

The scope can be nested:

The definition of local variables shields global variables. That is, names with smaller scopes take precedence over those with larger scopes.

 

Const:

Const converts an object into a constant. The const constant must be initialized during definition, because it cannot be modified later. By default, the const variable is the local variable of the file.

 

Extern:

1 declare a common variable 2 so that the const constant changes from the local variable of the file to the variable of the entire program.

 

Reference:

A reference is a composite type. The object of the same type as the reference must be used for initialization. (Key points: 1 must be initialized 2 use objects, not constant 3 use objects of the same type) reference is just an alias, a nickname.

 

Const reference:

References to the const object.

Const int ival = 209;

Cons tint & refival = ival;

 

Typedef:

Define a synonym for a type. No new types are introduced.

 

Header file:

It generally includes the definition of the class, the declaration of the extern variable, and the life of the function. To avoid multiple inclusion, use the Preprocessor to define header file protection characters.

# Ifndef salesitem_h

# Define salesitem_h

# Endif

 

 

 

________________ [: Variable type] __________________________________________________________

 

Basic Type:

Integer: Short, Int, long

Float Type: float, double, long double

Character Type: Char

Boolean:

Non-basic type:

Pointer


Array

Structure

Enumeration


Class

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.