Basic knowledge of computer basic units and details

Source: Internet
Author: User

Basic Unit Conversion Relationship

1 Byte = 8 Bit

1 KB = 1,024 Bytes

1 MB = 1,024 KB

1 GB = 1,024 MB

1 TB = 1,024 GB

1 PB = 1,024 TB

1 EB = 1,024 PB

1 ZB = 1,024 EB

1 YB = 1,024 ZB

 

PS: B refers to Byte, transliteration, Byte meaning,Generally, byte is used as the basic unit of computer storage capacity.. One Byte is eight binary bits (eight binary BITs constitute one Byte), that is, 1 Byte = 8 bits (a combination of eight bits, a total of 256 circuit states ). 1 digit = 1 English letter (character) = 1 Byte, that is, a standard English letter is a Byte, it has 8 bits ), (for example, if expression A is 10 days, it is represented by 00001010 and contains 8 binary digits ). 1 Chinese Character = 2 bytes, that is, a standard Chinese character has 2 bytes, 8 bits and 1 byte, that is, a Chinese character must be expressed in 16 bits in the computer. In addition, Chinese Punctuation Marks take up 2 bytes, and English Punctuation Marks (,.?! % & +-*/), 1 byte, Chinese ellipsis (......) And the (break) number (--) Each occupies 4 bytes.

 

Basic Types of C language:

 

32-bit Compiler

Char: 1 byte

Char * (pointer variable): 4 bytes (32-bit addressing space is 2 ^ 32, that is, 32 bits, that is, 4 bytes. Similarly, 64-bit compiler)

Short int: 2 bytes

Int: 4 bytes

Unsigned int: 4 bytes

Float: 4 bytes

Double: 8 bytes

Long: 4 bytes

Long: 8 bytes

Unsigned long: 4 bytes

 

64-bit Compiler

Char: 1 byte

Char * (pointer variable): 8 bytes

Short int: 2 bytes

Int: 4 bytes

Unsigned int: 4 bytes

Float: 4 bytes

Double: 8 bytes

Long: 8 bytes

Long: 8 bytes

Unsigned long: 8 bytes

 

Pointer variable operation summary:

 

Pointer variables are referenced in two ways: one is to reference pointer variables themselves, such as various operations on Pointer variables; the other is to use pointers to access the variables pointed, indirect reference to the pointer.

 

1.Pointer assignment

 

(1) Assign the variable address value to the pointer variable so that the Pointer Points to the variable.

The following definition is provided:

int a,b,*pa,*pb;float *pf;

 

The first line defines Integer Variables a, B and pointer variables pa, pb. Pa and pb have not been assigned values, so pa and pb do not point to any variable, 6.3 (). The following statement:

a=12;b=18;pa=&a;pb=&b;

 

The first row assigns values to variables a and B, and the second and third rows respectively assign the addresses of variables a and B to the pointer variables pa and pb, so that pa and pb point to the variables a and B respectively. In this way, variable a can also be expressed as * pa, and variable B can also be expressed as * pb. 6.3 (B.

 

(A) (B) (c)

Figure 6.3 pointer address assignment

 

(2) assignment between pointer variables of the same type

Both pa and pb are integer pointer variables, which can be assigned values to each other, for example, pb = pa. In this case, both pa and pb point to variable, a, * pa, * pb are equivalent. 6.3 (c.

Note: Only pointer variables of the same type can be assigned values to each other, such as pf = pa. Because pa is an integer pointer, pf is a floating point pointer.

 

(3) Assign "null" to the pointer variable

When a pointer variable is just defined, its value is uncertain and points to an uncertain unit. If a pointer variable is referenced at this time, unexpected consequences may occur and program or data may be damaged. To avoid these problems, in addition to assigning a fixed address value to the pointer variable, you can also assign a "null" value to the pointer variable, this pointer does not point to any variable.

 

The "NULL" pointer value is represented by NULL. NULL is a pre-defined constant in the header file stdio. h. Its value is 0. You should add a pre-defined line when using it, for example:

 

#include "stdio.h"pa=NULL;

 

You can also use the following statement to assign a null value to the pointer"

 

Pa = 0;

Or

Pa = '\ 0 ';

 

In this example, the pointer pa does not point to 0 address units, but has a definite "null value", indicating that pa does not point to any variable.

Note: although the pointer can be assigned 0 values, it cannot assign other constant addresses to the pointer. For example, even if you know that the address of integer variable a is 4000, you cannot use the following value assignment statement:

 

Pa = 4000; but only: pa = &;

For global pointer variables and local static pointer variables, if they are not initialized during definition, the compilation system will automatically initialize to NULL pointer 0. The local pointer variable is not automatically initialized, so the point is not clear.

 

Example 6.1Input two integers from the keyboard to a and B, and output them from large to small.

 

# Include <stdio. h> void main () {int a, B, * pa = & a, * pb = & B, * p;/* define pa, pb, and initialize, 6.4 (a) */scanf ("% d", & a, & B); if (* pa <* pb) {p = pa; /* For pointer exchange, 6.4 (B), as shown in (c) */pa = pb; pb = p;} printf ("\ n a = % d, B = % d \ n ", a, B); printf (" \ n max = % d, min = % d ", * pa, * pb ); /* pa points to a large number and pb points to a decimal number */}

 

 

If input: 12 22

Output result: a = 12, B = 22

Max = 22, min = 12

 

In this example, the pointer variable pa and pb are initialized, respectively pointing to the variables a and B. In the output, pa points to the large number, and pb points to the decimal number. Compare the size of a and B. in a hour, the pointer pa and pb are switched, so that pa points to the large number B, and pb points to the decimal number a, so as to meet the requirements in the question. Pointer variation 6.4.

(A) (B) (c)

 

Figure 6.4 pointer changes

 

2.Arithmetic Operations on pointers

 

A pointer can add or subtract an integer n, but the result is not directly add or subtract n from the pointer value, but is related to the Data Type of the object indicated by the pointer. The value (address) of the pointer variable should be "n × sizeof (pointer type )".

 

For example, the following definitions are available:

int *p,a=2,b=4,c=6;

 

Assume that the variables a, B, and c are allocated in a continuous memory zone. The starting address of a is 4000, as shown in 6.5 (.

(A) (B) (c)

Figure 6.5 pointer Movement

 

Statement p = & a; indicates that p points to the variable, that is, the content of p is 4000; 6.5 (B ).

Statement p = p + 2; the pointer moves down the positions of two integer variables. The value of p is 4000 + 2 × sizeof (int) = 4000 + 2 × 2 = 4004, instead of 4002, because the integer variable occupies two bytes, as shown in 6.5 (c.

 

We can intuitively understand that:

P = p + n indicates that p moves n storage unit blocks to the high address (one unit block refers to the pointerStorage space occupied by the specified variables).

P = p-n indicates that p moves n storage unit blocks to the lower address.

P ++, ++ p refers to moving the current pointer p to a storage unit of the high address.

If p ++ is used as the operand, p is referenced first, and then p is moved to a storage unit block in the direction of the high address, while ++ p moves the pointer first and then references p.

P --, -- p, the current pointer p moves a storage unit block to the low address.

If p -- is used as the operand, p is referenced first, and then p is moved to a lower address. -- P first moves the pointer and then references p.

 

3.Relational operation of pointers

Pointers can be used for relational operations. You can perform all relational operations on the two pointers in the relational expression. If p and q are pointer variables of the same type, p> q, p <q, p = q, p! = Q, p> = q are allowed.

Assume that p and q point to two pointers of the same Array and execute the p> q operation, which means that if the expression result is true (not 0 values ), it means that the elements referred to by p are after the elements referred to by q. Or q indicates that the element is closer to the first element of the array.

Note: before performing a relational operation on a pointer, the pointer must point to a fixed variable or storage area, that is, the pointer has an initial value. In addition, only pointers of the same type can be compared.

 

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.