I also want to learn C language-Chapter 10: global variables and local variables

Source: Internet
Author: User
Tags variable scope

I. Definition

Local variable: The variable defined in the function is called a local variable.

Global variables: variables defined outside the function are called global variables.

Variable scope: local variables only act inside the function, and global variables act on the entire project.

Ii. Differences between global variables and local variables

1: A local variable is valid only within the function that defines it. Other functions cannot use it (because each function has its own independent space and the space has been recycled when other functions use it ).

2: global variables are in the Data zone, and local variables are in the stack. Generally, global variables are automatically initialized to 0. Local variables are dependent on the compiling environment, while all C variables in debug are dependent on the residual values that previously occupied this space in Release mode. Debugging:

In this example, we can easily see that the global variable address is in 0x0042c17c (Data zone), and the local variable address is in 0x0012ff44 (stack Zone) (how to identify the zone from the address, I think this can only be a rough idea. That is to say, which address is probably the region, because after debugging several programs, we will see the characteristics of the address regions occupied by different variables !).

  Partitions in the memory when the application is running:Code zone (code can be read and executed); Data zone (global variables, static variables) (readable and writable); stack zone (local variables); heap zone; (My understanding is still incomplete. I will learn more in the future! I will explain it again !).

  3: Different initialization times. The global variable is initialized before the main function, and the local variable is initialized only when it is input to the function. For example:

That's it! We can see it clearly! The global variable g_x was initialized as the 0x12345678 value defined in my code before being added to the main function (sorry! I have not captured the modified Code diagram, and cannot see g_x = 0x12345678 ).
Q: How do I know the global variable g_x is 0x12345678 ?! We didn't see a value assignment statement during debugging!
Since we did not see the value assignment operation with global variables from the real entrance during debugging, the value assignment operation is still before. In this case, let's go to the executable file to see:

We can see that 024A33 has a 12345678 value. Is this the value of the global variable! Modify it to 0x87654321. Then let's look at the output of the executable file. The value of global variable g_n is also changed. This is an example! The global variables are initialized by the compiler. It is written in an executable file !!!
Note: Do not use global variables! Because global variables are related to too many "people" and "things!

Related Article

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.