Storage types of C variables and functions

Source: Internet
Author: User
Tags define local

Storage types of C variables and functions

In the previous article "C program memory management", I already know the storage structure of executable files compiled by C language and the memory layout during runtime, this article records the Storage types of variables and functions in C language and some behaviors in memory.

The Declaration/Definition Format of variables in C language is as follows:Storage Type modifier data type variable name; storage type: used to specify the storage location of the variable, that is, where the variable is run to allocate memory space, common storage locations include auto, extern, register, and static. In an execution program, you can allocate buckets for variables such as BSS, data zone, stack zone, and heap zone. Type modifier: used to modify the storage and representation of variables. Including long, short, signed, unsigned, void, const, volatile, etc. Data Type: it is used to specify the storage size of the variable, that is, the memory space occupied by a variable of this type. Basic data types include char, Int, float, and pointer types, and user-defined data types (struct, Enum, typedef, and union)
The Declaration format of functions in C language is as follows:Storage type return data type function name (parameter list) storage type: used to identify the function scope, rather than the storage location, mainly extern, static, auto and register do not need to represent the storage type of the program. Returned data type: the data type returned to the caller when the function exits.
1. Auto storage type

Auto can only be used to indicate the storage type of the variable, indicating the automatic type, indicating that the local variable is stored in the stack area of the running process. Generally, for local variables, auto is the default storage type. Therefore, it can be omitted without writing. Note: If a local variable is used directly without initialization, the system will randomly assign a value to it, which is not safe. Auto variables are initialized every time they are called.


2. Data about global variables and extern declarations

Extern can declare variables or identify functions. For declared variables, extern declares the global variables referenced in the current file in other files. Initialized global variables are stored in the data zone, so declaring global variables in other files will not allocate memory space for them.

The difference between declaring a variable and defining a variable:

Define a variable: Tell the compiler how much space it needs to allocate to the variable.

Declare a variable: tells the compiler to use the variable, but the variable is defined elsewhere.

For a function, the storage type only identifies the function scope. The default storage type is extern.

Two ways to declare global variables:

1. Define in the header file, and then reference the header file

2. define it in other files and use extern to reference the variable.


3. Register storage type

The register keyword can only define local variables. It can only modify integer or struct type, which mainly indicates variables that have been used for a long time. The local variables defined by register exist in registers for a long time. It is ideal that register variables are used for loop control. Like auto, if the register variable is directly used without initialization, the system will randomly allocate a variable.


4. static storage type

Static can identify both variables and functions. Variables defined as static (global variables and local variables) are stored in the data area, and their lifecycles are the whole program. If it is a static local variable, its scope is in its own {}. If it is a static global variable, its scope is the entire current file.

If the static variable is not initialized, the system will automatically initialize it to a value of 0 and it will only be initialized once.

The scope of the static function can only be the current file, so it cannot be called in other files. For functions that do not need to be called in other files, it is necessary to declare them as static, so as to avoid function name conflicts to some extent.


5. Constant data

Constant data is stored in the code segment. Its lifetime is the running time of the entire program, but its scope is the current file.




Storage types of C variables and functions

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.