C language 2 Data Types

Source: Internet
Author: User
Tags ranges


From http://www.programfan.com/article/showarticle.asp? Id = 2669

The variables can be described in three aspects:

· Data Type

· Storage Type

· Scope

In this lecture, we will only introduce the description of data types. Other instructions will be introduced later. The so-called data type is divided by the nature of the volume to be explained, the representation, the amount of storage space occupied, and the structural characteristics. In C language, data types can be divided into four categories: basic data type, constructed data type, pointer type, and null type.

1. Basic data type

The main feature of the basic data type is that its values cannot be decomposed into other types. That is to say, the basic data type is self-explanatory.

2. Construct a data type to construct a data type

It is defined based on one or more defined data types in a constructor. That is to say, the value of a construction type can be divided into several "members" or "elements ". Each "member" is a basic data type or a constructor type. In C, there are several types of constructor:

· Array type

· Structure Type

· Union type

3. pointer type

A pointer is a special and important data type. The value indicates the address of a certain amount of memory. Although the value of the pointer variable is similar to the integer value, the two types are completely different, so they cannot be confused. 4. When calling a function value of the null type, a function value should be returned to the caller. The returned function value has a certain data type and should be described in the function definition and function description. For example, in the max function definition provided in the example, the function header is: int max (int A, int B). The "int" type specifier indicates that the return value of this function is an integer. In another example, the library function sin is used. Because the system requires that the function return value be of Double Precision Floating Point type, in the Value assignment statement S = sin (x, s must also be a double-precision floating point to be consistent with the return value of sin function. Therefore, in the description section, the S type is double-precision floating point type. However, there is also a type of function that does not need to return the function value to the caller after calling. This function can be defined as "null type ". Its type description is void. In this section, we will first introduce the integer, floating point, and complex types in the basic data types. Other types will be introduced in subsequent lectures.

For the basic data type, whether the value can be changed is divided into two types: constant and variable. In the process of program execution, the amount of the value that does not change is called a constant, and the variable value is called a variable. They can be combined with data types for classification. For example, it can be divided into Integer constants, integer variables, floating-point constants, floating-point variables, character constants, character variables, enumeration constants, and enumeration variables. In a program, constants can be directly referenced without instructions, while variables must be described first and then used.

Integer

Integer types include Integer constants and integer variables. An integer constant is an integer constant. In C language, the Integer constants are octal, hexadecimal, and decimal.

Integer constant

1. The octal round constant must start with 0, that is, 0 is the prefix of the octal round number. The digital value ranges from 0 ~ 7. The octal number is generally an unsigned number.

The following values are valid Octal numbers:

015 (decimal: 13) 0101 (decimal: 65) 0177777 (decimal: 65535)

The following numbers are invalid Octal numbers:

256 (no prefix 0) 03a2 (including non-octal digital)-0127 (with a negative number)

2. hexadecimal Integers

The prefix of the hexadecimal integer constant is 0x or 0x. Its digital value ranges from 0 ~ 9, ~ F or ~ F.

The following numbers are valid hexadecimal integers:

0x2a (decimal: 42) 0xa0 (decimal: 160) 0 xFFFF (decimal: 65535)

The following numbers are invalid hexadecimal integers:

5A (no prefix 0x) 0x3 h (including non-hexadecimal digital)

3. Decimal integer constant

The decimal integer constant has no prefix. Its digital value ranges from 0 ~ 9.

The following numbers are valid decimal integers:

237-568 65535 1627

The following numbers are not legal decimal integers:

023 (no leading 0) 23D (contains non-decimal digits)

In a program, the prefix is used to differentiate various hexadecimal numbers. Therefore, when writing constants, Do not mistake the prefix to make the result incorrect. 4. The suffix of the integer constant is on a 16-digit machine, and the length of the basic integer is also 16 bits. Therefore, the range of the expressed number is also limited. The range of the unsigned integer constant in decimal is 0 ~ 65535. The number of symbols is-32768 ~ + 32767. The value range of the octal value is 0 ~ 0177777. The value range of the hexadecimal unsigned number is 0x0 ~ 0xffff or 0x0 ~ 0 xFFFF. If the number used exceeds the preceding range, it must be expressed as a long integer. The long integer is represented by the suffix "L" or "L. For example:

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

Octal Length Integer constant 012l (decimal 10) 077l (decimal 63) 0200000l (decimal 65536)

Hexadecimal Length Integer constant 0x15l (decimal: 21) 0xa5l (decimal: 165) 0x0000l (decimal: 65536)

  

There is no difference between the 158l long integer and the basic integer 158. But for 158l, because it is a long integer, the C compilation system will allocate four bytes of storage space for it. For 158, because it is a basic integer, only two bytes of storage space are allocated. Therefore, pay attention to the calculation and output formats to avoid errors. The unsigned number can also be indicated by a suffix. The unsigned number of an integer constant is suffixed with "U" or "U ". For example, 358u, 0x38au, and 235lu are all unsigned values. Prefix. The suffix can be used to indicate the number of different types at the same time. For example, 0xa5lu indicates the hexadecimal unsigned long integer A5, Which is 165 in decimal format.

Integer variable

Integer variables can be divided into the following types:

1. Basic Type

The type description is int, which occupies 2 bytes in memory. The value is an integer constant.

2. Short integer

Type Description: Short Int or short 'c110f1. The bytes and value range are the same as the basic type.

3. Long Integer

The type description is long Int or long, which occupies 4 bytes in memory. The value is an integer constant.

4. unsigned

The type description is unsigned.

The unsigned type can match the above three types:

(1) The unsigned basic type specifiers are unsigned Int or unsigned.

(2) The unsigned short Integer type description is unsigned short.

(3) The unsigned long integer type description is unsigned long.

The memory space bytes occupied by various unsigned types are the same as the corresponding signed types. However, the negative number cannot be expressed because the symbol bit is omitted. The following table lists the memory bytes allocated and the ranges of integer types in Turbo C.

Number of bytes to be allocated in the range of type specifiers

INT-32768 ~ 32767 ■

Short int-32768 ~ 32767 ■

Signed int-32768 ~ 32767 ■

Unsigned int 0 ~ 65535 ■

Long int-2147483648 ~ 2147483647 ■

Unsigned long 0 ~ 4294967295 ■

Description of Integer Variables

The variables are generally described as follows: type specifier variable name identifier, variable name identifier,...; for example:

Int A, B, C; (a, B, c is an integer variable)

Long X, Y; (X and Y are long integer variables)

Unsigned p, q; (p, q is an unsigned integer variable)

Note the following when writing the variable description:

1. Multiple variables of the same type can be described after a type specifier. Use commas to separate variable names. There must be at least one space interval between the type specifier and the variable name.

2. The last variable name must end.

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

[Practice] // 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

D, 2,100

Of vtable

'Vupdate

1, 0; 2, 0

3, 0

4,100

1, 80

2,180

3,360

4,200

Of vupdate

Of Practice

[Practice] // 2int A = 5;

Int B = 9;

Long int C;

Long D;

C = a + B-7;

D = a * B * C;

C = D * D;

A = c-d; 'vtable

A, 2, 5

B, 2, 9

C, 4, 0

D, 4, 0

Of vtable

'Vupdate

1, 5

2, 9

3, 0

4,0

3, 7

4,315

3,31255875

1.-5112

Of vupdate

Of Practice

[Practice] // 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

Of vtable

'Vupdate

1, 6; 2, 19

3, 0

4,0

3,65530

4,-114

1.-101

2,101

Of vupdate

Of Practice

Void main (){

Long X, Y;

Int A, B, C, D;

X = 5;

Y = 6;

A = 7;

B = 8;

C = x +;

D = Y + B;

Printf ("c = x + A = % d, D = Y + B = % d \ n", C, D );

}

Return void for "Main", that is, no value of any type is returned.

X and Y are defined as long

A, B, C, and D are defined as int type.

5-> X

6-> Y

7->

8-> B

X + A-> C

Y + B-> d

Displays the running result of long X, Y;

Int A, B, C, D;

C = x +;

D = Y + B;

From the program, we can see that X and Y are long integer variables, and A and B are basic integer variables. Operations can be performed between them, and the calculation result is a long integer. But C, D is defined as a basic integer, so the final result is a basic integer. This example shows that different types of quantities can be involved in calculation and assigned values to each other. The type conversion is automatically completed by the compilation system. The type conversion rules will be described later.

Capacity

Real Constants

Real instances are also called floating point instances. Real constants are also called real numbers or floating-point numbers. In C, the real number only uses decimal. It has two forms: decimal number form index form

1. decimal number format

From digital 0 ~ 9 and decimal point. For example, 0.0,. 25, 5.789, 0.13, 5.0, 300, and-267.8230 are valid real numbers.

2. exponential form

It consists of a decimal number, an order sign "E" or "e", and an order code (which can only be an integer and can be signed. Generally, the form is a e n (A is a decimal number, and N is a decimal integer). The value is a * 10, and N is like 2.1e5 (equal to 2.1*10, 5 ), 3.7e-2 (equal to 3.7*10,)-2 *) 0.5e7 (equal to 0.5*10, 7),-2.8e-2 (equal to-2.8*10,)-2 *) the following is not a valid real number 345 (no decimal point) E7 (no number before level sign E)-5 (NO level sign) 53. -E3 (incorrect position of negative signs) 2.7e (NO level code)

Standard C allows floating point numbers to use suffixes. The suffix "F" or "F" indicates the number of floating point numbers. For example, 356f and 356. are equivalent. Example 2.2 illustrates this situation:

Void main ()

{

Printf ("% F \ n", 356., 356f );

}

Void indicates that main does not return any value and ends with the result displayed in printf.

Real Variables

There are two types of real variables: Single-precision and double-precision. The type specifiers are float Single-precision descriptors and double-precision descriptors. In Turbo C, the single-precision model occupies 4 bytes (32-bit) memory space. The value range is 3.4e-38 ~ 3.4e + 38. Only seven valid digits are allowed. The dual-precision model occupies 8 bytes (64-bit) memory space, and the value range is 1.7e-308 ~ 1.7e + 308, which provides 16 valid digits.

The format and writing rules of real variables are the same as those of integer types.

For example: Float X, Y; (X, Y is the actual type of single precision)

Double A, B, C; (a, B, c is the actual amount of Double Precision)

Actual constants are processed in double type regardless of single or double precision.

Void main (){

Float;

Double B;

A = 33333.33333;

B = 33333.33333333333333;

Printf ("% F \ n", a, B );

}

This program indicates the difference between float and double.

A ■

B ■

A <--- 33333.33333

B <--- 33333.33333333333 ;;

Show Program Results

This program indicates the difference between float and double.

Float;

Double B;

A = 33333.33333;

B = 33333.33333333333333;

In this example, we can see that a is a single-precision floating point type, and the number of valid digits is only seven. The integer has five digits, so after the decimal two digits, all digits are invalid. B is a dual-precision model with a valid bits of sixteen bits. However, Turbo C requires that a maximum of six digits be retained after decimal places, and the remaining parts are rounded down.

[Practice] // floatint A = 32;

Float B;

Double D;

B = 12345678;

D = B * 100;

D = d +;

D = d + 58.123456; 'vtable

A, 2, 32

B, 4, and 0.0

D, 8, 0.0

Of vtable

'Vupdate

1, 32

2, 0

3, 0

2, 12345678.00000

3,1234567800

3,1234567832

3, 1234567890.123456

Of vupdate

Of Practice

[Practice] // 1int A = 543;

Float B;

B = 123.123962 +;

B = b-100;

A = B; 'vtable

A, 2,543

B, 4, and 0.0

Of vtable

'Vupdate

1,543

2, 0.0

2,123.123962

2, 23.123962

1, 23

Of vupdate

Of Practice

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.