C ++ primer Study Notes (2.0) variables and Basic Types

Source: Internet
Author: User

 

Chapter 2 variables and Basic Types

2.1 Basic built-in types

In basic built-in types, the maximum and minimum values of these types vary depending on the number of digits.

That is, the storage space of the C ++ basic data type depends on the machine ."

Type

Description

Minimum storage space

Bool

Boolean

-

Char

Character Type

8bit

Wchar_t

Wide character type

16bit

Short

Short integer

16bit

Int

Integer

16bit

Long

Long Integer

32bit

Float

Single precision floating point type

6-digit valid number 32bit

Double

Dual-precision floating point

10-bit valid number 64bit

Long double

Extended precision floating point type

10-bit valid number 64bit

C ++ does not specify the number of bytes for each data type, but specifies that the number of bytes for int type data is not greater than that for long int type data and not less than that for short type data.

The storage space of C ++'s basic data types is essentially different from that of the CPU.

The C ++ compiler has an unwritten convention that defines the int Length Based on the machine length, which best reflects the code efficiency. Not all compilers follow this rule. To ensure that the value of the basic data type in the program does not cross the border, you must understand the compiler you are using.

Generally, in Visual C ++, int occupies 4 bytes, short occupies 2 bytes, and long occupies 4 bytes or 8 bytes.

 

Experience: 1. In most machines, integer int is used for counting, and short is rarely used.

2. Float types such as double are usually not error-free. It has 10 decimal places, and float with only 6 decimal places may not be enough.

 

 

2.2-character nominal value constant

20 // decimal

024 // octal

0x14 // hexadecimal format // The value above indicates 20 in decimal format.

Adds a suffix to change the type of a literal constant.

128u // unsigned

1l // long, l can be in upper case and lower case, but it is recommended to use upper case

3.1415f // float type with single precision, where F can be in upper case and lower case, F can only appear after decimal places, and integer cannot!

3.1415e0f // 3.1415*10 ^ 0 and float type

'A' // char

L 'A' // wchar_t

Escape characters

Escape characters

Meaning

ASCII value (decimal)

\

Bell (BEL)

007

\ B

Backspace (BS)

008

\ F

Change pages (FF)

012

\ N

Line feed (LF)

010

\ R

Press enter (CR)

013

\ T

Horizontal tabulation (HT)

009

\ V

Vertical tabulation (VT)

011

\\

Backslash

092

\?

Question mark character

063

\'

Single quotes

039

\"

Double quotation mark characters

034

\ 0

Null)

000

\ Ddd

Any character

Three octal nodes

\ Xhh

Any character

Two hexadecimal

Of course, it can also be expressed in ASCII code.

For example, \ 7 indicates a bell.

String Literal Value "Hello world! "Takes 13 bytes, and an empty '\ o' character is automatically added at the end.
Just like the wide character nominal value, l "Hello world! "Is the wide string literal value, which occupies 26 bytes. It is automatically followed by a" \ 0 "wide null character.

Strings of the same type can be connected, but strings of the same type can be wide. strings of the same type are not defined!

For example, you can write cout <"A muw.line" in this way"

"String literal"

"Using concatenation"

<Endl;

Note: The Literal Value of a string written in two lines must be followed by a backslash \

Exercise 2.10

#include <iostream>using namespace std;int main(){cout<<"2M"<<endl;return 0;}

After modification

#include <iostream>using namespace std;int main(){cout<<"2\tM\n";return 0;}

2 ^ 10

#include <iostream>using namespace std;int main(){int value=2,pow=10,res=1;for(;pow>0;pow--) {res=res*value;}cout<<"the result of 2^10 is "<<res<<endl;return 0;}

Exercise 2.11

#include <iostream>using namespace std;int main(){int value,pow,res=1;cout<<"please enter two numbers(value and power):"<<endl;cin>>value>>pow;int pow1;pow1=pow;for(;pow>0;pow--) {res=res*value;}cout<<"the result of "<<value<<"^"<<pow1<<" is "<<res<<endl;return 0;}

2.3 Variables

Left and right

The left value can be placed on the left or right of the value pair, while the right value can only be placed on the right.

The left value is the address of the variable or an expression that represents the position of the object in the memory. the right value is the value, constant, or constant variable of the variable.

For example, val1 = val2/8 // val1 is the left value and val2 is the right value!

The expression is not a left value !!

The object is a zone of type in memory!

A variable name consists of numbers, subtitles, and underscores. It must start with a letter or underscore !! The variable name should not be too long. It cannot be a keyword in C ++ ~!! The following are several important C ++ keywords:

 

Picture from http://apps.hi.baidu.com/share/detail/30620027 details see this site.

Define object (initialization)

Initialization is divided into: 1 replication initialization such as: int A = 1024;

2 direct initialization, for example, int A (1024); // more flexible and efficient direct initialization!

Remember, Initialization is not a value assignment. Initialization is to create a variable and assign it a new value, while a value assignment is to erase the original value and replace it with the new value.

Built-in variable initialization rules: defined in the function body and automatically initialized to zero. In vitro, the function is not initialized !!

We recommend that you initialize each built-in object !!

Class initialization: 1 has a default constructor. For example, string is a null character by default.

2. Other definitions that must be explicitly defined

Reference

A const reference is a reference to a const, and a non-const reference is a reference to a non-Const!

Related Article

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.