Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Why do you want to add a type modifier to the basic data type of C++
Why do you want to add a type modifier to the basic data type of C++
The basic data types are: plastic, real, character, pointer, etc.

The extended data types are: array, structure, union, * * object, etc.

You can also have custom types.

The following is a detailed description of the basic types

It is best to find a book in C language, whether it is a paper version or an electronic version. Suggest to look at Tan Weiqiang's C program design. The latest edition is the third edition, which was the second edition when I was studying.

integer quantity

Integer quantities include integer constants and integer variables. An integer constant is an integer constant. In C language, there are three kinds of integer constants: octal, hexadecimal and decimal.

Integer constant

1. Octal integer constant must start with 0, that is, 0 is used as the prefix of octal number. Digital value is 0 ~ 7. Octal numbers are usually unsigned numbers.

The following numbers are legal octal numbers:

0 15 (decimal13) 010/(decimal 65)0 177777 (decimal 65535)

The following numbers are not legal octal numbers:

256 (without prefix 0) 03A2 (including non-octal numbers) -0 127 (with negative sign)

2. Hexadecimal integer constant

Hexadecimal integer constants are prefixed with 0X or 0x. Its numerical values are 0~9, A~F or a ~ f.

The following numbers are legal hexadecimal integer constants:

0X2A (42 in decimal 42) 0XA0 (decimal 160)0x ffff (decimal 65535)

The following numbers are not legal hexadecimal integer constants:

5A (without prefix 0X) 0X3H (including non-hexadecimal digits)

3. Decimal integer constant

Decimal integer constants have no prefix. Its number is 0 ~ 9.

The following numbers are legal decimal integer constants:

237 -568 65535 1627

The following numbers are not legal decimal integer constants:

023 (without leading 0) 23D (including non-decimal digits)

In the program, all kinds of decimal numbers are distinguished by prefixes. Therefore, when writing constants, don't get the prefix wrong, which will lead to incorrect results. 4. The suffix of the integer constant is on the machine with the word length of 16 bits, and the length of the basic integer is also 16 bits, so the range of the number represented is also limited. Decimal unsigned integer constants range from 0 to 65535, and signed numbers range from -32768 to +32767. The range of octal unsigned number is 0 ~ 0 177777. The expression range of hexadecimal unsigned number is 0x0 ~ 0xffff or 0x0 ~ 0xffff. If the number used exceeds the above range, it must be expressed as a long integer. Long integers are denoted by the suffix "l" or "l". For example:

Decimal long integer constant 158L (decimal 158) 358000L (decimal -358000)

Octal long integer constant 0 12L (decimal 10) 077L (decimal 63) 0200000L (decimal 65536)

Hexadecimal long integer constant 0X 15L (decimal 2 1)0xA5l (decimal 165)0x 10000 l (decimal 65536).

There is no numerical difference between the long integer 158L and the basic integer constant 158. But for 158L, because it is a long integer, the C compiler system will allocate 4 bytes of storage space for it. For 158, because it is a basic integer, only 2 bytes of storage space are allocated. So pay attention to the operation and output format to avoid mistakes. Unsigned numbers can also be represented by suffixes, and the suffix of unsigned numbers with integer constants is "u" or "u". For example, 358u, 0x38au and 235lu are all unsigned numbers. Prefixes and suffixes can be used to represent different types of numbers at the same time. For example, 0XA5Lu represents a hexadecimal unsigned long integer A5, and its decimal number is 165.

Integer variable

Integer variables can be divided into the following categories:

1. basic type

The type specifier is int, which occupies 2 bytes in memory and its value is a basic integer constant.

2. Short integers

The type specifier is short int or short' c110f1. The range of bytes and values occupied is the same as that of the basic type.

3. Long integers

The type specifier is long int or long, occupying 4 bytes of memory, and its value is long integer constant.

4. Unsigned types

The type specifier is unsigned.

Unsigned types can match the above three types:

(1) The unsigned primitive type specifier is unsigned int or unsigned.

(2) The unsigned short integer type specifier is an unsigned short integer.

(3) The unsigned long integer specifier is an unsigned long integer.

Various unsigned types occupy the same number of bytes in memory space as the corresponding signed types. However, because the sign bit is omitted, it cannot represent a negative number. The following table lists the number of memory bytes allocated by various integer quantities in Turbo C and the representation range of this number.

Number of bytes allocated for the type descriptor range

int -32768~32767 ■■

Short integer -32768~32767 ■■

Signed int -32768~32767 ■■

Unsigned integer 0~65535 ■■

long int-2 147483648 ~ 2 147483647■■■

Unsigned long integer 0~4294967295 ■■■

Description of integer variables

The general forms of variable description are: type descriptor, variable name identifier, variable name identifier, ...; For example:

int a,b,c; (A, B and C are integer variables)

Long x, y; (x, y are long integer variables)

Unsigned p, q; (p, q are unsigned integer variables)

When writing variable descriptions, you should pay attention to the following points:

1. allows multiple variables of the same type to be described after the type specifier. Variable names are separated by commas. There is at least one space between the type descriptor and the variable name.

2. The last variable name must be followed by ";" The number ends.

3. The variable description must be placed before the variable is used. Usually placed at the beginning of the function body.

[exercise] // 1int a, b;

Short int c;;

Short d =100;

a = d-20;

b = a+d;

c = a+b+d;

d = d-a+c-b; Vtable

A, 2, 0

b,2,0

c,2,0

2 100

Tabular

Vupdate

1,0; 2,0

3,0

4, 100

1,80

2, 180

3,360

4,200

Vupdate

practical

[exercise]///2 int a = 5;;

int b = 9;

long int c;

Long d;

c = a+b-7;

d = a * b * c;

c = d * d * d

a = c-d; Vtable

A, 2.5

b,2,9

c,4,0

d,4,0

Tabular

Vupdate

1,5

2,9

3,0

4,0

3,7

4,3 15

3,3 1255875

1,-5 1 12

Vupdate

practical

[Exercise] //3int a=6, b =19;

Unsigned int c;;

int d;

c = a-b+ 7;

d = b * c;

a = b+ c+d;

b =-a; Vtable

A, 2, 6

b,2 19

c,2,0

d,2,0

Tabular

Vupdate

1,6; 2, 19

3,0

4,0

3,65530

4,- 1 14

1,- 10 1

2, 10 1

Vupdate

practical

void main(){

Long x, y;

int a,b,c,d;

x = 5;

y = 6;

a = 7;

b = 8;

c = x+a;

d = y+b;

printf("c=x+a=%d,d=y+b=%d\n ",c,d);

}

Explain that main returns void, that is, it does not return any type of value.

X and y are defined as long.

A, b, c and d are defined as int types.

5->; x

6->; y

7->; a

8->; b

x+a-& gt; c

y+b-& gt; d

Display the running results of programs with long x and y axes;

int a,b,c,d;

c = x+a;

d = y+b;

As can be seen from the program, X and Y are long integer variables, and A and B are basic integer variables. Operations are allowed between them and the result is a long integer. But c and d are defined as basic integers, so the final result is a basic integer. This example shows that different types of quantities can participate in operations and assign values to each other. Among them, the type conversion is automatically completed by the compilation system. The rules about type conversion will be introduced later.

real number

Real constant

Real type is also called floating-point type. Real constants are also called real numbers or floating-point numbers. In C language, only decimals are used for real numbers. It has two forms: decimal number form and exponential form.

1. Decimal number form

It consists of numbers 0~ 9 and decimal point. Such as 0.0, 0.25, 5.789, 0. 13, 5.0,300. , -267.8230 are all legal real numbers.

2. Exponential form

It consists of a decimal number, a sequence code mark "e" or "e" and a sequence code (integers and signed only). Its general form is a E n (a is a decimal number and n is a decimal integer), and its value is a * 10, where n is: 2. 1E5 (equal to 2.1*10,5), 3.7E-2 (.

Standard c allows floating-point numbers to use suffixes. The suffix "f" or "f" indicates that the number is a floating point number. Such as 356f and 356. Are equivalent. Example 2.2 illustrates this situation:

void main()

{

printf("%f\n%f\n ",356。 ,356 f);

}

Void means that main does not return any value. End with printf displaying the results.

real variable

Real variables are divided into single precision and double precision.

Its type specifiers are floating-point single-precision specifiers and double-precision specifiers. In Turbo C, the single-precision type occupies 4 bytes (32 bits) of memory space, and its numerical range is 3.4e-38 ~ 3.4e+38, and only 7 significant digits can be provided. Double-precision type occupies 8 bytes (64 bits) of memory space, and its numerical range is1.7e-308 ~1.7e+308, which can provide 16 significant digits.

The format and writing rules of real variable description are the same as those of integers.

For example: float x, y; (x, y are single-precision real numbers)

Double a, b, c; (A, B and C are double-precision real quantities)

Real constants, whether single precision or double precision, are regarded as double precision and double type.

void main()

{

Floating a;

Double b;

a = 33333.33333

b = 33333.33333333333333

printf("%f\n%f\n ",a,b);

}

This process explains the difference between float and double.

a ■■■

b ■■■■■■■■

a & lt- 33333.33333

b & lt- 33333.33333333333; ;

Show program results

This process explains the difference between float and double.

Floating a;

Double b;

a = 33333.33333

B = 33333.333333 From this example, it can be seen that because A is a single-precision floating-point type, the significant digit is only 7 digits. Moreover, the integer has occupied five places, so the two decimal places are invalid. B is a double-precision type with 16 significant bits. However, Turbo C stipulates that at most six digits are reserved after the decimal point, and the rest are rounded off.

[exercise]///float int a = 32;;

Floating b;

Double d;

b = 12345678;

d = b * 100;

d = d+a;

d = d+58. 123456; Vtable

A, 2, 32

b,4,0.0

8,0.0

Tabular

Vupdate

1,32

2,0

3,0

2, 12345678.00000

3, 1234567800

3, 1234567832

3, 1234567890. 123456

Vupdate

practical

[exercise]/1int a = 543;

Floating b;

b = 123. 123962+a;

b = b- 100;

a = b; Vtable

Answer, 2543

b,4,0.0

Tabular

Vupdate

1,543

2,0.0

2, 123. 123962

2,23. 123962

1,23

Vupdate

practical

Number of characters

Character quantity includes character constant and character variable.

Character constant

Character constants are characters enclosed in single quotation marks. For example,' a',' b',' =','+','?' Is a legal character constant. In C language, character constants have the following characteristics:

1. character constants can only be enclosed in single quotation marks, not in double quotation marks or other brackets.

2. Character constants can only be a single character, not a string.

3. This character can be any character in the character set. But after numbers are defined as character types,

Can't participate in numerical operations. For example,' 5' and 5 are different. "5" is a character constant and cannot participate in the operation.

Economic and Social Committee

The escape character is a special character constant. Escape characters begin with a backslash "\" followed by one or more characters. Escape characters have a specific meaning, which is different from the original meaning of characters, so they are called "escape" characters. For example, in the previous example, the "\n" used in the format string of the printf function is an escape character, which means "carriage return and line feed". Escape characters are mainly used to represent control codes that are inconvenient to express with ordinary characters.

Common escape characters and their meanings

Escape character the meaning of the escape character.

\n carriage return and line feed

\ tJump laterally to the next tabulation position.

\v vertical tabs

\b backspace key

\r input.

\f Press paper to change pages.

\ \ backslash "\"

Single quotation mark'

\a ring the doorbell.

\ DDD65438+ 0 ~ 3 characters represented by octal digits.

\ xhh 1 ~ 2 Characters represented by hexadecimal digits.

Broadly speaking, any character in the C language character set can be represented by escape characters. For this reason, it is recommended to use \ddd and \xhh in Table 2.2. Ddd and hh are octal and hexadecimal ASCII codes respectively. Such as the word 10 1? Quot\XOA ",102 means the letter" b ",134 means a backslash, 134 means a line break and so on. Use of escape characters

void main()

{

int a,b,c;

a = 5; b = 6; c = 7;

printf(" % d \ n \ t % d % d \ n % d % d \ t \ b % d \ n ",a,b,c,a,b,c);

}

This program practices the use of escape characters.

A, b and c are integers 5->; I. 6->b, 7->c

Call printf to display the running results of the program

printf(" % d \ n \ t % d % d \ n % d % d \ t \ b % d \ n ",a,b,c,a,b,c);

The program is "\n" after outputting a value of 5 in the first column, so it returns to a new line; Then "\t", so jump to the next tabulation position (set the interval of tabulation position to 8), and then output the b value of 6; Empty the second cell and output a value of 7, followed by "\n", so a new line is returned; Output value 5 after the second space; Empty three cells again and output the value of b 6; After the second time, "\t" jumps to the next tab position (aligned with 6 in the previous line), but the next escape character "\b" makes it return a space, so the value of c is output next to 6.

Character variable

The value of a character variable is a character constant, that is, a single character. The type specifier of a character variable is char. The format and writing rules of type description of character variables are the same as those of integer variables.

For example:

char a,b; Each character variable is allocated a byte of memory space, so only one character can be stored. Character values are stored in the storage unit of variables in the form of ASCII codes. Like x

The decimal ASCII code is 120, and the decimal ASCII code of y is 12 1. Assign "X" and "Y" values to character variables A and B: A = "X"; B =' y Actually, the binary codes of 120 and 12 1 are stored in two units: A0 1 1000.

b 0 1 1 1 1 0 1

So they can also be regarded as integral quantities. C language allows assigning character values to integer variables and integer values to character variables. When outputting, it is allowed to output character variables as integers and integers as characters. The integer number is two bytes and the character number is one byte. When processing integer numbers according to the number of characters, only the lower octets participate in the processing.

Master ()

{

char a,b;

a = 120;

b = 12 1;

printf("%c,%c\n%d,%d\n ",a,b,a,b);

}

a ■ b ■

a & lt- 120

b & lt- 12 1

Show program results

This program shows that A and B are character types, but they are given integer values in the assignment statement. From the results, the output form of a and b values depends on the format symbols in the format string of printf function. When the format symbol is "C", the corresponding output variable value is a character, and when the format symbol is "D", the corresponding output variable value is an integer.

void main()

{

char a,b;

a = ' x

b = ' y

a = a-32;

b = b-32;

printf("%c,%c\n%d,%d\n ",a,b,a,b);

}

A and b are described as character variables and given character values.

Turn lowercase letters into uppercase letters.

Output as integers and characters.

In this example, A and B are described as character variables and given character values. C language allows character variables to participate in numerical operations, that is, using ASCII codes of characters to participate in operations. Because the ASCII codes of uppercase and lowercase letters differ by 32 bits, lowercase letters become uppercase letters after operation. And then output in the form of integers and characters respectively.

[Exercise]//charinta = 49;

char b;

char d;

b = a+ 10;

d = a+b; Vtable

Answer, 249

B, 1, random

D, 1, random

Tabular

Vupdate

1,49

2, random

3. Random

2,';'

3,' l '

Vupdate

practical

[Exercise] //char c 1, C2;

c 1 = ' a '; c2 = ' b

c 1 = c 1-32; C2 = C2-32; Vtable

C 1, 1, random

C2, 1, random

Tabular

Vupdate

1, random; 2, random

1,' a '; 2,' b '

1,' A '; 2,' B '

Vupdate

practical

String substitution

A string constant is a sequence of characters enclosed in a pair of double quotes. For example, "China", "C program:" and "$ 12.5" are legal string constants. String constants and character constants are different quantities. There are mainly the following differences between them:

1. Character constants are enclosed in single quotes, and string constants are enclosed in double quotes.

2. Character constants can only be a single character, while string constants can contain one or more characters.

3. Character constants can be assigned to character variables, but string constants cannot be assigned to character variables. There is no corresponding string variable in C language.

This is different from the basic language. But you can use character arrays to store string constants. It is introduced in the chapter on arrays.

4. Character constants occupy one byte of memory space. The number of bytes in memory occupied by a string constant is equal to the number of bytes in the string plus 1. The character "\ 0" (ASCII code is 0) is stored in the added bytes. This marks the end of the string. For example, the string "C program" occupies memory bytes: C program \0. Although both the character constant' a' and the string constant' a' have only one character, the situation in memory is different.

A' occupies one byte in memory, which can be expressed as

"A" occupies two bytes in memory, which can be expressed as a\0 symbolic constant.

Symbolic constant

In C language, a constant can be represented by an identifier, which is called a symbolic constant. Symbolic constants must be defined before use, and their general form is:

# Define identifier constants

#define is also a preprocessing command (preprocessing command is? Quot# "), known as the macro definition command (which will be further introduced in Chapter 9 preprocessor), is used to define the identifier as a constant value later. Once defined, constant values will replace all identifiers that appear in the program. Traditionally, the identifiers of symbolic constants are in uppercase letters, and the identifiers of variables are in lowercase letters to indicate the difference.

# Define PI 3. 14 159

void main()

{

float s,r;

r = 5;

s = PI * r * r

printf("s=%f\n ",s);

}

The macro definition command defines PI as 3. 14 159s, and r as the real number 5->; rπ* r * r-& gt; s

Display program results float s, r; r = 5; S = PI * r * r Before the main function of this program, the macro definition command defined PI as 3. 14 159, instead of PI in the program. S=PI*r*r is equivalent to s = 3. 14 159 * r * r It should be noted that the symbolic constant is not a variable, and the value it represents cannot be changed in the whole range. In other words, in a program, it can no longer be reassigned with an assignment statement.