Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define local variables and global variables in C language

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to define local variables and global variables in C language". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "how to define local variables and global variables in C language" can help you solve the problem.

Local and global variables

Generally speaking, variables can be defined in C language programs in the following places:

(1) the beginning of the function body.

(2) formal parameter variables in the header of the function.

(3) in the compound statement inside the function body.

(4) the outside of the function.

According to the position of variables defined in the program, variables can be divided into "local variables" and "global variables"

Variables defined within a function are called local variables. The first part of the function body, the formal parameters in the function header and the variables defined in the compound statement in the function body all belong to local variables.

The scope of a local variable is from the definition statement of the variable to the end of the innermost curly braces that contain the variable definition statement.

The variable defined outside the function in the source program file is called the global variable, the starting point of the global variable scope is the location of the variable definition, and the end of the source program file where it is located in the end of the global variable scope.

How variables are stored

In C language, the memory space for user programs to run is divided into three parts: program area, static storage area and dynamic storage area.

(1) the program area stores executable program instructions.

(2) the static storage area stores the variables that occupy the fixed memory unit during the running of the program, including the global variables and static local variables (static) defined in the program.

(3) the dynamic storage area stores the variables of the storage unit dynamically according to the needs during the running of the program, including the non-static local variables defined in the program, the formal parameters of the function and so on.

The storage categories of variables in C language can be divided into four categories: automatic (auto), static (static), register (register) and external (extern).

Storage category of local variables

There are three storage types for local variables: automatic (auto), static (static), and register (register).

1. Automatic local variable

The local variables in the function, if not specifically declared as static (static) storage categories, are dynamically allocated and released by the system, and the variables are stored in the dynamic storage area, which is called automatic variables. The parameters in the function are also automatic variables.

two。 Static local variable

If you want the value of the local variable in the function to retain the original value after the end of the function call, that is, the system does not release the memory space occupied by the variable, the variable maintains the value after the last function call on the next function call. At this point, you should use the static keyword to describe the local variables as static storage.

3. Register variable

In general, variables are stored in memory space, and when variables are used in the program, the instructions issued by the controller send the data from the value of the variables in memory to the operator, and the results produced by the calculator are sent to memory from the arithmetic unit if it needs to be saved. If some variables in the program are used frequently, the memory access time of the data will affect the efficiency of the program.

C language allows frequently used variables to be stored in registers to improve the efficiency of the program.

Storage category of global variables

Global variables are stored in static storage, and their lifetimes are fixed, which is the whole process of running the program. There are two main storage categories for global variables: external (extern) and static (static).

1. External global variable

If you want to use global variables defined in other source program files in one source program file, you need to use the keyword extern description.

Note: use defined global variables instead of defining new global variables, as shown in the P135 example.

two。 Static global variable

In programming, if you want the global variables defined in a source program file to be used only in this file, but not in other source programs, you can add the static keyword description when defining the global variables.

This is the end of the introduction on "how to define local variables and global variables in C language". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report