C Storage class, link summary

Source: Internet
Author: User

Refer to "C primmer Plus"

First, the C language keyword:

Type of data 14:

void, char (1 bytes), int (4 bytes), Short (2 bytes), Long (4 bytes), Signed (4 bytes), unsigned (4 bytes),

Float (4 bytes), double (8 bytes), struct, union, enum, typedef, sizeof

Storage class control type 6:

Auto, static, extern, register, const, volatile

Process Control 12 X

If, else, for, does, while, switch, case, default, return, Contiue, break, goto

Then look at the C language memory allocation form:

①BSS segment: Storing uninitialized global data and static data (static storage)

② Data segment: Storing initialized global variables (static store)

③ Code Snippet/text segment/code area: storage class member functions, global functions, and other functions

④: Stores the variables and addresses associated with the calling function, local variables that are temporarily created by the called function (excluding static declared variables)-when the function is called, the arguments are also pressed into the initiating call process stack.

After the call is finished, the function return value is also stored back into the stack

⑤ Heap: Storage of dynamically allocated variables

C provides 5 different storage models for variables, or storage classes. See table below:

Storage class

Storage Period

Scope

Link

How and where to declare

Automatic Automatic code block Empty links Within the code block
Register Automatic code block Empty links Within the code block, use the keyword register
Static with external links Static File scope External links Outside of all functions
Static with internal links Static File scope Internal links All functions, use the keyword static
Static for empty links Static code block Empty links Within the code block, use the keyword static

Storage classes can be described by the scope, link, and storage period of a variable.

1. Scope

Scopes are used to describe one or more areas where an identifier can be accessed (visible).  The scope of a C variable can be a code block scope, a function prototype scope (function prototype is a function declaration, that is, does not contain a function body), or a file scope. Where the code block is a piece of code that is contained within the opening curly brace and the corresponding closing curly brace. Variables defined outside of all functions have a file scope, and variables with a file scope are visible from the defined location to the end of the file.

2. Links

A C variable has one of the following links: external links, internal links, or empty links.

A variable with a code block scope or a function prototype scope has a null link, that is, the variable is private by the code block or function prototype. A variable with a file scope, or an internal link if using the storage type descriptor static, otherwise external. The internal link variable can be used anywhere in the file and is not visible outside the file, so there is no conflict with the variable with the same name as the other file (if the link is called with a redefinition error); The external link variable can be used in the file and in other files (declared with extern).

3. Storage Period

A C variable has 2 storage periods: a static storage period and an automatic storage period.

Variable with file scope, the keyword static indicates the link type, not the storage period, and all file scope variables have a static storage period. Static refers to the position of the variable is fixed.

Static variables with external links:

The definition and declaration of a variable are placed outside of all functions, i.e. an external variable is created. To make the program clearer, you can declare it again using extern in a function that uses an external variable, and if a local variable with the same name as an external variable is defined within the function, the variable is a separate automatic variable that overwrites the file scope variable with the same name. If a variable is defined in another file, it is necessary to declare the variable using extern (extern indicates a referential declaration)

where static variables and external variables are typically initialized at the time of the definition declaration, and if not initialized, are automatically initialized to 0

Small example:

//File1.cpp#define_crt_secure_no_warnings#include<iostream>using namespacestd;intCoal =1214;intABC = One;//File2.cpp#define_crt_secure_no_warnings#include<iostream>using namespacestd;intErrupt = -;/*Externally defined variables*/ Doubleup[ -];/*externally-defined arrays*/extern intCoal;//int abc = 10; /* Redefine with file1.cpp */voidNextvoid) {     intErrupt =Ten; printf ("%d\n", Errupt); }intMain () {printf ("%d\n", coal);    Next (); printf ("%d\n", Errupt); extern intErrupt;/*Optional Declarations*/       extern intUp[];/*Optional Declarations*/printf ("%d\n", Errupt); return 0; }

The output is:

1024

10

20

20

More expansion http://blog.csdn.net/hackbuteer1/article/details/7487694

C Storage class, link summary

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.