C ++ learning 3rd-Data Types

Source: Internet
Author: User

1. Basic addressing and variable Declaration

1) Addressing memory

In the previous introduction to variables, we know that the variable is the name of a region in memory. The independent unit of the memory is called the address, and the smallest addressable unit of the memory is called the byte.

For example, a sequential storage address:


Because all the data in the computer is expressed in bits, we use the data type to represent the data. C ++ has eight basic data types.

2) Declare Variables


You can assign values when declaring: explicit () and implicit ()

Variables of the same type can be declared in the same line:


But the following is incorrect:



3) The appropriate C ++ declarative style is: where and when needed. However, it must be declared before use.

2. Keyword and identifier naming

C ++ has 63 keywords, that is, reserved words:


Identifier naming rules:

A) It cannot be a keyword;

B) It can only consist of letters, numbers, and underscores;

C) It can only start with a letter or underline;

D) C ++ is case sensitive;

Recommended:

1) The name must start with lowercase:


2) for multiple words, the first letter of the second word starts with uppercase:


3) The name should actually reflect its meaning:


3. Variable Size and sizeof Operator

1) variable size refers to the number of bytes stored for this type of variable, that is, byte;

For example, a char: 1 byte; A bool: 1 byte; a short: 2 bytes; a int: 4 bytes; a long: 4 bytes; a float: 4 bytes; A double: 8 bytes;

2) The variable size can be obtained through sizeof (type:


4. INTEGER (interger)

Integer data stores only integers.

1) there are four types: Char, short, Int, long

2) There are two formats: signed and unsigned.


The number of unsigned objects is twice that of signed objects;

The signed range is-(2 ^ (n-1) to (2 ^ (n-1)-1, and the unsigned range is: 0 to (2 ^ N) -1.

Except for the char type, all the other three int types are signed by default.

3) Size of Class 8:


5) overflow-overflow occurs when the value assignment is out of the variable value range;

For example, 65535 + 1 = 0; 32767 + 1 =-32767

6) If an integer is divided, the decimal point is lost.

5. Floating Point Number

A floating point number is a real number, which contains integers and decimals.

1) three types: float, double, long double


A floating point number must have at least one decimal point:


Scientific Identification of floating point:


2) precision: the default cout output is 6 bits, float is 7 bits, and double is 16 bits. You can set the output precision through setprecision () of iomanip.

3) Rounding Error: for example, 0.1 may be 0.0999999999999

Because there is an integer error, the floating point number comparison may be incorrect.

6. boolean type

Boolean: True/false, non-zero/0;


The value can be passed! To change;

7. Character Type

Characters can represent smaller numbers or ANSI characters.


Char output is a character by default, and (INT) char can output numbers. Int type output characters are carried out using (char) int;

2) Escape Character:


8. Constant

Constants are numerical constants and character constants.

1) character constants, defined by macros


You can also use the const Keyword:


2) When being defined through const, values must be assigned and cannot be changed later.

9. Hungarian naming law

The Hungarian name method was invented by Charles Simonyi of Hungary in 1972. A prefix is added to the identifier.


Although it adds a naming burden, it can also clearly mark its type.

2) Includes 3 parts: range flag + type identifier + type Prefix:




[Disclaimer:
1) This content may come from the Internet, or I have sorted it out by myself. It only represents the opinions and opinions of the Internet and individuals!
2) This content is for reference only. Any reference to this content will cause any consequences and will be irrelevant to the original author and the author of this blog.]

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.