"C Language" local variables, global variables, local static variables, global static variables, extern,static differences

Source: Internet
Author: User

Local variables:

Created on the stack (growing down), the life cycle exists in the current scope.

is a random value if it is not initialized at the time of creation.

Global variables:

Created in the static zone, the life cycle is always present.

0 if it is not initialized at the time of creation.

Static local variables:

The external link properties are severed. Created in the static zone.

0 if it is not initialized at the time of creation.

The storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another adjacent area.

Global static variables:

The external link attribute is cut off and created in the static zone, and the life cycle persists.

0 if it is not initialized at the time of creation.

#include <stdio.h> #include <stdlib.h>int fun1 () {static int n = 0;    n++; return n;}    int main () {int ret = 0;        for (int i = 0; i < ten; i++) {ret = FUN1 ();    printf ("%d\n", ret);    } system ("Pause"); return 0;}

Output-------... 10.

#include <stdio.h> #include <stdlib.h>static int n;int fun1 () {n = 0;    n++; return n;}    int main () {int ret = 0;        for (int i = 0; i < ten; i++) {ret = FUN1 ();    printf ("%d\n", ret);    } system ("Pause"); return 0;}

Output 10 x 1.


extern

External file can be detected with external link properties.

Static

Cut off external link properties.

Variables located in the static zone:

The entire life cycle exists, such as global variables, static variables.

#include <stdio.h> #include <stdlib.h>int b = 20;int Main () {static int a = 10;    int c = 5;    printf ("%p\n", &a);    printf ("%p\n", &b);    printf ("%p\n", &c);    System ("pause"); return 0;}

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7A/D8/wKiom1a-jrqBc9_PAABPx4qeNu0367.png "title="%4m) FK {5~ijfhpdbiafjeg2.png "alt=" Wkiom1a-jrqbc9_paabpx4qenu0367.png "/>

It can be concluded that the storage address of the static variable and the global variable is very close, in the same region, namely: are stored in the static zone.

This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1741752

C language Local variables, global variables, local static variables, global static variables, extern,static differences

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.