Data Storage types: auto, static, register, and extern

Source: Internet
Author: User

Each variable in the C language has two attributes: Data Type (integer, floating-point, complex type) and data storage type, which are auto and static ), register (Register) and external (extern ). The following describes them one by one:

Auto type: in fact, all local variables in the function are auto variables without special declarations, but the keyword "Auto" can be omitted. These variables are allocated with storage when the function is called. After the function is called, these buckets are released.

Static type: static declared variables are static variables. After a function call is completed, these variables do not disappear, but the current data is retained, the value of the variable in the next call is the value after the last call is completed. For example:

int func(){static int val = 10;return --val;}int main(void){for(int i = 0; i < 10;++i)printf("%d\n",func());return 0;}

The output values are 9, 8, 7, 6, 5, 4, 3, 2, and 1, 0 in sequence.
This is only the most basic usage. In fact, static has two other functions:
(1) If static is added to a global variable, this variable can only be used in this file, rather than globally:

// Test. C // global variable char c = 'a'; // test. hextern char C; // main. C # include "test. H "# include <stdio. h> int main (void) {printf ("% C", c); Return 0 ;}

We defined the global variable char C in test. C and can use it in Main. C. (The extern declaration in the header file will be discussed later .) However, if we add static to the global variable in test. C, C can only be used in test. C.
(2) initialize the variable to 0. In fact, it is not only static, but also the global variables are automatically initialized to 0. This is because they are in the static storage area, where all the byte defaults are 0x00:

#include <stdio.h>int val;int main(void){static int a;printf("%d,%d",val,a);return 0;}

The print result is 0, 0.

Register Type: the Register modifier implies that the variables of the Compilation Program will be frequently used. If possible, it should be stored in the CPU register to speed up its access. However, the Register modifier has the following restrictions:
(1) only local automatic variables and formal parameters can be used as register variables, and other variables (such as global variables) cannot.
(2) The number of registers in a computer system is limited. You cannot define any number of register variables.
(3) Local static variables cannot be defined as register variables.
In fact, this variable is out of date, because the current computer processing speed is fast enough, so it is rarely used ..

Extern type: it is not a definition, but a declaration. It indicates that the definition of this variable or function is in another file. We can see from the previous example. In C, the function is defined as extern by default.

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.