Variable type and storage mode of C language

Source: Internet
Author: User

Variables can be divided into global variables, static global variables, local variables, and static local variables

There are two scenarios for declaring a variable:

1. One is the need to create a storage space (a declaration of definition). For example, int A has built up storage space at the time of life.

2, the other is not required to create storage space (declarative declaration). For example, extern int a where variable A is defined in another file.

Partition of Memory Area:

1. Stack area: The memory area that is automatically allocated and freed by the compiler for storing function parameter values, local variables, etc.

2. Heap Area: Programmers apply or release to the system.

3, Global zone: Used to save global variables and static variables.

4. Literal constant area: The area of memory used to hold the constant string.

5, program code area: used to save the function body of the binary code.

Let's look at an example:

The fileOne.h header files are as follows:

#ifndef dmtestvariable_fileone_h#define dmtestvariable_fileone_h//declares global variables extern int globalvariable;void addOne (); void Teststaticglobal (); #endif

The fileone.c file is as follows:

#include <stdio.h> #include "fileOne.h"//static global variable staticglobalvariable = 12;void AddOne () {//global variable plus 1 g Lobalvariable + = 1;}    void Teststaticglobal () {//static global variable plus 1 staticglobalvariable + = 1; printf ("Static global variable:%d\n", staticglobalvariable);}

The Main.c file is as follows:

#include  <stdio.h> #include   "fileOne.h"//  Declaration of global Variables int globalvariable = 10; Void test () {    //  static local variables     static int  staticlocalvariable = 20;    staticlocalvariable += 1;     printf ("Static local variable Value:%d\n", staticlocalvariable);} Int main (int argc, const char * argv[]) {    //  local variable test     int localvariable = 15;    printf ("local variable%d\n", localvariable);        //  global variable Test     addone ();     printf ("Global variable:%d\n", globalvariable);         //   Testing of static local variables     for  (int i = 0 ; i < 2;  i++)  {        test ();    &nBSP;}         //  Static global Variables     for  (int i  = 0 ; i < 2; i++)  {         teststaticglobal ();     }    return 0;}

The output results are as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/45/DF/wKiom1Ps0piCTWLbAAChqedg8CQ794.jpg "title=" Variable type and store. png "alt=" wkiom1ps0pictwlbaachqedg8cq794.jpg "/>

Summarized as follows:

Global variables, with global scope, simply defined in one source file, can be used for all source files, and of course other source files that do not contain global variable definitions need to be declared again with the extern keyword, as in code

Definition declaration global variable int globalvariable = 10;

Global variables, defined in the MAIN.C source file, when used in the fileone.c source file, have the following code declared again

declares global variables extern int globalvariable;


Static global variables, which can only be used in functions within the file module in which they are located, cannot be accessed by functions in other file modules. As in the code

Static global variable staticglobalvariable int = 12;

Can only be used in fileone.c source files and cannot be used in MAIN.C source files.


Local variables: can only be used within the body of the function that defines the variable. As in the code

local variable test int localvariable = 15;

Can only be used in the main function.


Static local variables: defined within a function, but when a function exits, a static local variable is always present, and the memory unit it occupies is not freed until the end of the program. Although the static local variable still exists after exiting the function, it cannot be used, and if the function that defines the static local variable is called again, it can continue to be used, and the value left after the last call is saved. As in the code

static local variable static int staticlocalvariable = 20;

This static local variable value of the first call to the test () function output is 21, and the second call to the function test () outputs this static local variable value of 22.

This article is from the "7803002" blog, please be sure to keep this source http://7813002.blog.51cto.com/7803002/1540247

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.