C language various variable storage way

Source: Internet
Author: User

1. Variable type:

C language variables are divided into local variables and global variables.
1. Local Variables:
A variable that is defined inside a function or inside a block (that is, a pair of curly braces) [all of which is called a block], and is only valid for block scope.
Features: Different blocks can have the same name variable name, representing different variables, non-interference; Function parameters are also local variables.
2. Global variables:
Variables defined outside the function are valid from the beginning of the definition to the end of the file, and can of course be extended to other files using the extern adornment.
Features: If the local variable has the same name as the global variable, the external variable does not work within the scope of the local variable, and it is not necessary to use the global variable as much as possible, since it reduces the versatility of the function.

2. Experiment

Plus the static and const keywords, how are these variables stored? The following code is passed in the Linux system debug:

#include <stdio.h>#include <stdlib.h>#include <unistd.h>  intInt_a=1; StaticintStatic_int_a=2; ConstintConst_int_a=3;intMain () {intint_b=1; Staticintstatic_int_b=2; Constintconst_int_b=3;int *P1=malloc (4);printf("int_a: %p\ n", &int_a);printf("static_int_a: %p\ n", &static_int_a);printf("const_int_a: %p\ n", &const_int_a);printf("Int_b: %p\ n", &int_b);printf("Static_int_b: %p\ n", &static_int_b);printf("Const_int_b: %p\ n", &const_int_b);printf("P: %p\ n", &AMP;P1);printf("main: %p\ n", main);printf("%d\ n", Getpid ()); while(1);}

is the debug result:

is the maps file when the program runs:

3. Results

Through the above code experiment can be seen
1. Global variables for normal global variables and static modifiers are stored in the global zone (which is stored in the stack when the program runs), but the const-modified global variables are stored in the code area (the data exists in read-only memory while the program is running).
2. Local variables of ordinary local variables and const modifiers are stored in the stack, but static decorations are stored in the global zone.

4. Miscellaneous Notes

One, const modifier
1. The modified variable must be initialized:

constint i=1;//合法    constint j;//非法

2. The modified variable can only be read:

constint i=1;    int j=2;    i=j;//非法    j=i;//合法

3. You can avoid unnecessary memory allocations:

#define STR "abcdef"    constchar str[]="abcdef";        printf(STR);//为STR第一次分配内存    printf(str);//为str一次分配内存    printf(STR);//为STR第二次分配内存    printf(str);//已经不需要分配内存了    
同样是一个常量字符串,宏定义在编译时候自动替换了,所以实际是很多的常量。

4. Numeric constants and pointer constants are distinguished:

    int x=1;    constint y=2;          //y是常量,初始化之后就不能修改y值了    constint *p1=&x;       //p1指向的内容是常量,不可以修改    intconst p2=&x;      //指针p2是常量,所指向的内容可修改    constintconst p3=&x;//指针p3和p3指向的内容均不可以修改    p1=&y;//合法    *p2=3;//合法

5.const constants may not necessarily be modified

   constint x=1;    int *p=(int*)&x;    *p=2;//   此时的x的值便被修改为2了

C language various variable storage way

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.