C Language Chapter 6

Source: Internet
Author: User

Directory of this document

    • One, byte and address
    • Second, the storage of variables
    • Third, the binary form of negative numbers
    • Iv. Scope of variables
    • V. Initialization of variables

The use of variables is briefly described in the previous section, and when we define a variable, the system allocates a piece of storage space for the variable. The value of the variable is stored in memory in binary form, which is to delve into some of the storage details of variables in memory.

Back to top one, byte and address

To better understand the storage details of variables in memory, first recognize the "bytes" and "addresses" in memory.

1. The memory in the computer is the storage space in bytes. Each byte of memory has a unique number, which is called an address. As if the hotel is a room unit, each room has a unique room number, we can find the corresponding room according to room number.

The inside of each small box represents a byte in memory, the white number is the address of each byte (here to take the hexadecimal to display, the address value is random, only as a reference, the actual situation of the address value is not necessarily this), you can find that the address of adjacent bytes in memory is continuous.

2. As you all know, a byte has 8 bits, and the range of data that can be represented is very limited, so a large range of data takes up more than one byte, that is, different types of data take up the same number of bytes.

Back to top ii. storage of variables 1. The role of variable types

As with other programming languages, the C language uses variables to store the values used by the calculation process, and any variable must first define the type and then use it. Why must we define it first? Because the type of the variable determines the storage space that the variable occupies, the variable type is defined to allocate the appropriate storage space for the variable to hold the data. For example, char type, which is used to store a character, a character only needs 1 bytes of storage space, so the system will only assign a char type variable 1 bytes, no need to allocate 2 bytes, 3 bytes or more storage space.

2. How much storage space does a variable occupy

1> The storage space that a variable occupies is not only related to the variable type, but also to the compiler environment. The same type of variable, in different compiler environment occupies the storage space is not the same. We all know that the operating system has a different number of digits, such as Win7 32-bit, 64-bit, the compiler is the same, there are different bits: 16-bit, 32-bit, 64-bit (Mac system clang compiler is 64bit). Since we are developing under the MAC system, we are using a 64-bit compiler as our standard.

2> The following table describes the storage space used by the basic data types in a 64-bit compiler environment, and it is helpful to learn about the pointers and arrays in the future.

3> The following table describes the storage footprint for different compiler environments

3. Examples of variables

When a variable is defined, the system allocates a certain amount of storage space for that variable.

1 int Main () 2 {3     char a = ' a '; 4     5     int b = 10;6     7     return 0;8}

1> in a 64bit compiler environment, the system allocates 1 bytes, 4 bytes of storage units for the variables A and B, respectively. Other words:

    • The 10 in variable b is stored in 4 bytes, 4 bytes Total 32 bits, so variable b should be stored in memory as 0000 0000 0000 0000 0000 0000 0000 1010.
    • The ' a ' in variable A is stored in 1 bytes, 1 bytes are 8 bits, and variable A is stored in memory as 0100 0001, as to why the binary of ' a ' is so, and then discussed later.

2> the above variables A and b are stored in memory roughly as shown in the following table:

(Note: A small lattice in the "Stored Content" column represents a single byte, and the "Address" column refers to the address of each byte)

    • As you can see, variable B takes up 4 bytes of memory address from FFC1~FFC4, and variable A takes up 1 bytes of memory address of FFC5. Each byte has its own address, but the variable also has an address. The address of the first byte of a variable storage unit is the address of the variable. The address of variable A is FFC5, and the address of variable B is ffc1.
    • Memory addressing is large to small, which means that everything starts with a large byte of memory address, so the system prioritizes the variable with a large address value. Since the variable A is defined first and the variable b is defined, you will see that the address of variable a FFC5 is larger than the address of variable B ffc1.
    • Note the contents of variable b stored in the table, the binary form of variable B is: 0000 0000 0000 0000 0000 0000 0000 1010. Because memory addressing is from large to small, it is from the memory address the largest byte start to store data, the storage order is FFC4-FFC3, FFC2-FFC1, so the front of 0000 0000 are placed in ffc2~ffc4, the last side of the eight-bit 0000 1010 put it in the FFC1.

4. View the memory address of the variable

During the debugging process, we often take a print to see the address of the variable

1 #include <stdio.h>2 3 int main () 4 {5     int a = 10;6     printf ("The address of variable A is:%p", &a); 7     return 0;8}

The & In line 6th is an address operator, and &a represents the address of the variable A. The format character%p is specifically used to output addresses. The output is:

The address of variable A is: 0x7fff5fbff8f8

This 0x7fff5fbff8f8 is the memory address of variable A.

Back to top three, negative numbers in binary form
1 int Main () 2 {3     int b = -10;4     

In line 3rd, an integer variable is defined, and its value is-10. How is 10 stored in memory? In fact, any value is stored in the form of complement in memory.

    • The complement of positive numbers is the same as the original code. Like 9, the original code and the complement are 1001.
    • The complement of a negative number equals its positive number and the original code is reversed after +1. (The inverse means 0 change 1, 1 to 0)

Then-the 10 complement calculation process is as follows:

1> first calculates the binary form of 10:0000 0000 0000 0000 0000 0000 0000 1010

2> Reverse 10 binary: 1111 1111 1111 1111 1111 1111 1111 0101

3> results of reverse +1:1111 1111 1111 1111 1111 1111 1111 0110

Therefore, the integer-10 binary form in memory is: 1111 1111 1111 1111 1111 1111 1111 0110

Back to top four, scope of the variable 1. Introduction to Scopes

The scope of a variable refers to the range of the variable. Let's take a look at the following program:

1 int Main () 2 {3     int a = 7;4     5     return 0;6}
    • A variable A is defined in line 3rd, and when executed into this line of code, the system allocates storage space for variable a
    • When the main function is executed, that is, the 5th line of code is executed, the memory occupied by variable A will be automatically reclaimed by the system
    • Therefore, the scope of the variable A is from the line that defines it, until it ends in the curly brace {}, which is the 3rd to 6th line, and once you leave this range, the variable a fails.

2. Code block

The 1> code block is actually a piece of code enclosed in curly braces {}.

1 int Main () 2 {3     {4         int a = ten; 5          6         printf ("a=%d", a); 7     } 8      9     a = 9;10     one     re Turn 0;12}
    • Note the curly braces on line 3rd to 7th, which is a block of code
    • When you execute to line 4th, the system allocates memory to the variable a
    • When the code block executes, that is, when the 6th line of code is executed, the memory occupied by variable A is reclaimed by the system
    • Therefore, the scope of the variable A is from the line that defines it, until it ends in the curly brace {}, which is the 4th to 7th Line, leaving the range, and the variable a fails.
    • Therefore, the above program is compiled failed, the 9th line of code is wrong, variable A in line 7th has been invalidated, it is not possible to use the 9th line

2> If this is the case

1 int Main () 2 {3     int a = 9; 4      5     {6         int a = ten; 7          8         printf ("a=%d", a); 9     }10     one     re Turn 0;12}
    • Note the 3rd, 6 lines, each defining a variable a, which is OK. The C language stipulates that variables with the same name are allowed in different scopes, and the system allocates different storage space for them.
    • The scope of the variable a defined in line 3rd is: line 3rd to 12th; The scope of variable a defined in line 6th is: 6th to 9th Row.
    • Finally, note Line 8th: Try to output the value of variable A. What is the variable A that is output here? First look at the output:
a=10

Here is the "nearest principle", that is, the 8th row accesses the variable a defined in line 6th, not the variable a in line 3rd.

Back to top v. Initialization of variables

The variable is not used until it is initialized, because it stores some junk data.

1 #include <stdio.h>2 3 int main () 4 {5     int c;6     7     printf ("%d", c); 8     return 0;9}

Note that the variable C of line 5th simply defines the variable and does not assign an initial value to it. Output Result:

1606422622

It can be found that the variable C stores some messy data.

C Language Chapter 6

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.