Introduction to variable Storage types in C Language

Source: Internet
Author: User

The C language describes variables in two aspects: variable types and Storage types of variables. Variable types such as int (integer) and char (character type) indicate the memory space occupied by the variable. The variable storage type is used to describe the scope of the variable.

C language variable Storage types include automatic class, register class, static class and external class.

A local variable is a variable (sometimes called an automatic variable) described in the function. It is described using the keyword "auto. All non-full variables are considered local variables, so auto is never used. Local variables are automatically generated when the function is called, but are not automatically initialized. As the function call ends, the variable disappears automatically. This variable is automatically generated when the function is called the next time, and the value is assigned again, it automatically disappears when you exit.

Static variables are declared with the keyword static. Variable types can be divided into static local variables and static full-process variables.

  1. Static local variables
  2. It differs from a local variable in that this variable always exists when the function exits, but cannot be used by other functions. When you enter the function again, the last result is saved. Others are the same as local variables.

  3. Static full-process variable
  4. Static full-process variables are variables that are visible only in the defined source file but not in other source files. It differs from full-Process Variables in that full-process variables can be described as external variables (extern) and used by other source files, but static full-Process Variables cannot be described as external variables, that is, it can only be used by the source file.

    • External variables are declared with the keyword extern. To enable variables to be used by other files in addition to defining their source files, we need to notify every program module file of the full variables. In this case, extern can be used to describe the variables.
    • Register variables are usually used with greater emphasis on execution speed. The idea is to tell the compiler to put the variable in a CPU register. Because data operations in registers are faster than in memory, this increases the execution speed of program code. Register variables include the keyword register before the variable name and type. It is worth noting that the bitwise operator cannot act on register variables.

The following code analyzes the types of variables.

Declare I as a full-process variable at the beginning of file1.c and initialize it to 1. In the main function, two automatic variables I and j are described. In this way, the I that appears inside the main function is the automatic variable.

File1.c

#include <stdlib.h> #include <stdio.h> int reset(); int next(); int last(); int sum(int ); int i=1; void main() {     auto int i,j;     i=reset();     for(j=1;j<=3;j++)     {         printf("i=%dtj=%dn",i,j);         printf("next(i)=%dn",next());         printf("last(i)=%dn",last());         printf("sum(i+j)=%dn",sum(i+j));     } } 

The file2.c file defines variable I at the beginning and declares it as a static variable. Therefore, it is only used in the current file. However, from the definition of the sum function, the I used internally is a form parameter, and j is an internal static variable, which is different from the previous I and j.

File2.c File

static int i=10; int next() {     return(i+=1); } int last() {     return(i-=1); } int sum(int i) {     static int j=5;     return(i=j+=i); } 

At the beginning of file3.c, it indicates that I is external, indicating that it is the same variable as I defined by file1.c. Therefore, the reset value returns the current value of I 1.

File3.c File

extern int i; reset() {     return(i); } 

The specific execution process is analyzed as follows:

  1. First, the program is compiled and generated into an executable file. After the executable file is run, the function enters from the Main function body. during initialization, the I is a global variable and the initial value is 1.
  2. After entering the main function, call the function reset () to enter the file file3.c. The file file3.c declares that I is an external variable. Therefore, if the value of I is 1, the returned value is 1. After the function reset () is executed, it returns to the main function and continues to execute the program in the loop body.
  3. The loop body first calls the function next (), the program enters the file2.c file, and determines that I is a static variable. Therefore, the value of I is 10, and the value of I is 11 after the next () is executed, the return value is 11, and 11 is used as the value of I. When the last () function is called, the value of I is 11. After the function is executed, the value of I changes to 10 and 10 is used as the value of I. Finally, execute the sum (I) function. At this time, note that the parameter is passed during the execution of the program, that is, the actual calculated value for the first call is sum (I + j = 2 ), in this way, the execution result after the sum (I) function is called is I = 5 + 2 = 7, that is, the execution result.
  4. And so on, respectively, 7, 10, and 14. Through the above program, we can understand that different storage types have different scopes. in programming, how to flexibly use different storage types can make the program more flexible.

Appendix: running result

i=1 j=1 next(i)=11 last(i)=10 sum(i+j)=7 i=1 j=2 next(i)=11 last(i)=10 sum(i+j)=10 i=1 j=3 next(i)=11 last(i)=10 sum(i+j)=14

Parse static keywords

Static can be used to modify variables or functions in c.

Let's first look at the time when it is used to modify variables. Variables in c can be divided into global data areas, stacks, and stacks. In fact, the stack we usually call is a stack that does not contain a pair. Don't confuse it.

int a;main(){int b ; int c* = (int *)malloc(sizeof(int));}

A is a global variable, B is a stack variable, and c is a stack variable.

Static modification to the global variable can be considered as limiting that the variable can only be referenced in this file. Some programs are composed of many. c files. Variables can be referenced by each other. However, after static modification, the variable can only be referenced by functions in this file.

Static modification of stack variables can be considered to extend the life cycle of stack variables to the end of program execution. In general, the life cycle of stack variables is managed by OS. During the rollback process, the life of stack variables ends. However, after static modification, the variables are not stored in the stack, but stored together with global variables. At the same time, it cannot be used after the function that defines it is left blank. However, if you call the function that defines it again, it can continue to be used and save the value left after the previous call.

Static Function Modification is similar to global variable modification. It can only be called by functions in this file, but cannot be called by functions in other files of the same program.

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.