Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What are variables, integer variables and real variables? Wait online. , make up points
What are variables, integer variables and real variables? Wait online. , make up points
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 (decimal 42)0xa 0 (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. On the machine, the suffix of integer constant is 16 bits, and the length of basic integer is 16 bits, so the range of numbers represented is 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 shortc110f1. 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