[C ++] Constant

Note: The following excerpt from [1], the modified part.

1. Constants: constant value can not be changed, its general form it can be determined whether a literal constant.

2. Constant comprising numerical constants (i.e., constant) and character constant.

 

 

 

 

 

 3. Constant no unsigned type. But a non-negative integer can be assigned to unsigned variable, as long as it does not exceed the scope of the variable value range. For example, the 50,000 assigned to a unsigned short int variable is possible, but the 70,000 assigned to it is not enough (overflows).

4. character constant

 

( 1) \ ddd: The character 1 to 3 octal number represents. \ xhh: 1 to 2 represents the character hexadecimal number represents. The '\ 101' represents ASCII character 101 in octal, and {$ (101) _8 $} = {$ (65) $ _ {10}}, Now ASCII table, it can be seen typifying character "A".

(2) Note that '\ 0' or '\ 000' is representative of the ASCII control character is 0, i.e., "empty operator", which is widely used in the string.

(3)  the escape character while comprising two or more characters, but it represents a character. In the build system to see the character "\", it will then find the characters following it, handling it as a character in memory only the first byte.

The character data is stored in the form and method of use in memory

ASCII character constants stored in its memory unit, such as a character variable c1 is the value 'a', is stored in a variable is 'a' ASCII codes 97 actually stored in binary form, i.e., 01100001. Since character data, its storage on a similar form and stored as an integer in ASCII code storage. Thus, it can be common among character data, integer data and C ++. A character data can be assigned to an integer variable, on the contrary, an integer data can also be assigned to a character variable. You can be doing arithmetic on character data, this time the equivalent of their ASCII codes for arithmetic operations.

 6. String constants : such as: "Hello", "abc"and the like. Constant string "abc" is 4 bytes (instead of three bytes) in the memory, as follows

Character 'a' binary form 'B' character in binary form Character 'c' in binary form \0

Finally, the compiler will automatically add a character string: '0' as a symbol of the end of the string. But the '\ 0' is not part of the string, it only marks the end of a string. The cout << "abc" << endl; output: abc total of three characters.

Specific examples : string constant "abc \ n" contains several characters? Consisting of four symbols, '\ n-' it is an escape character, but the amount usually 5 bytes string (containing a '\ 0' characters). "abc \\ n" five characters, representing six bytes.

7.  symbolic constants

 #define PRICE 30;    

PRICE is the symbolic constants. Symbolic constants used more in the C program, used often variable (see in C ++ variable ) and less symbolic constant.

 

references

[1] Hemopurification .C ++ programming [M] Beijing: Tsinghua University Press.

 

 

Guess you like

Origin www.cnblogs.com/chen-hw/p/11614209.html