In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to use auto and register keywords in C language". Xiaobian shows you the operation process through actual cases. The operation method is simple and fast and practical. I hope this article "how to use auto and register keywords in C language" can help you solve the problem.
I. Keyword classification
How many keywords does C have? In general, there are 32 books (including this book), but this is the standard of C90(C89). In fact, five new keywords were added after C99. However, the current mainstream compiler, C99 support is not good, we later default to use C90, that is, think 32
II. Supplementary content
Before we start talking about keywords, we need to understand the following basic concepts
1. Classification of variables
Variables are divided into global variables and local variables
Local variables: variables defined in a block of code are called local variables. Local variables are temporary. Enter the code block, automatically form local variables, exit the code block automatically released. [A lot of people on the Internet say that variables in functions are local variables, which cannot be said wrong, but the statement is inaccurate]
Global variables: variables defined outside all functions are called global variables. Global variables are global.
Note: Code block-In a function, the area enclosed by {} is called a code block. Code blocks can be nested.
2. Scope and life cycle of variables
Scope concept: refers to the area of code where the variable can be accessed normally
Scope of global variables: valid for the entire program
Scope of a local variable: Valid only within the block of code in which the local variable resides
Life cycle concept: refers to the time range from the definition of the variable to the release of the space. The so-called release refers to the "released" space once opened up.
Lifetime of a global variable: Once defined, the variable remains valid for the entire life of the program
Lifetime of local variables: enter code block, form local variables [open space], exit code block,"free" local variables
#includeint g_val = 10; //g_val is defined outside all functions and is a global variable int main(){ int a = 20; //a is defined inside the main function and is a local variable printf("%d\n", g_val); printf("%d\n", a); return 0;}#includeint g_val = 100;int main(){ int x = 10; if (x == 10) { int y = 20; pritnf("%d %d", x, y); //can } pritnf("%d %d", x, y); //error, y can only be accessed inside if code block}#include int g_x = 100; //global variable void show(){ printf("show: global: %d\n", g_x); //can be accessed in any code block}int main(){ show(); printf("main: global: %d\n", g_x); //can be accessed and even modified in any code block return 0;}#includeint g_x = 100; //global variable int main(){ int g_x = 10; //local variable, same as global printf("g_x:%d\n", g_x); //Output is partial, that is, partial and all have the same name, partial priority. return 0;}
Summary: Scope is a spatial concept that indicates the area where the variable can be effectively accessed or used.
Life cycle is the concept of time, indicating when the variable space is opened and when it is released.
When local and global variables are repeated, local variables take precedence (proximity rule)
The most generous keyword- auto
1, how to use: variables generally defined in the code block, that is, local variables, the default is auto modified, but generally omitted, but not all variables are auto modified by default, auto is generally only used to modify local variables
2, usage: auto keyword is relatively old, generally we can omit directly when defining variables
Summary: auto is used to modify local variables, indicating that the life cycle and scope of the local variable are only valid within the code block, can be omitted, and cannot be used to modify global variables.
#includeauto int b = 10; //error, auto cannot be used to modify the global variable int main(){ auto int a = 30; //Equivalent to int a = 30;}
4. The fastest keyword- register1. Storage classification
In the computer interior, the closer the memory unit to the CPU runs faster, but its relative unit manufacturing cost is also higher, the farther the memory unit from the CPU runs slower, the unit cost is lower, in order to minimize the cost, to achieve maximum CPU operating efficiency, there is a storage classification method.
2. Register
The reason for the existence of registers: CPU is mainly responsible for the calculation of the hardware unit, but in order to facilitate the operation, the general first step needs to read the data from the memory to the CPU, then it also needs the CPU to have a certain temporary storage capacity of data, but the CPU is not currently to calculate, only to read the specific data into the CPU, because that is too slow. Therefore, modern CPUs are integrated with a set of hardware called registers, which are used to store temporary data.
The essence of register existence: at the hardware level, improve the computing efficiency of the computer. Because you don't have to read data from memory anymore.
3. Register modifier variable
Register modifier variable role: try to modify the variable, into the CPU register area, so as to achieve the purpose of improving efficiency
Register modifies the efficiency essence of variables: placing variables in registers so that the CPU can operate directly on the data without reading it from memory.
Since variables modified with register can improve efficiency, is it best to modify all variables with register? The answer, of course, is no. Because the number of registers is limited, heavy use of register modifiers can reduce program efficiency. So what variables should be modified by register?
(1) Local (global causes CPU registers to be occupied for a long time)
(2) will not be written (writing data requires reloading the data into memory, which also loses the meaning of putting the data into the register)
(3) Need to be read at high frequency (read the data directly from the register to improve efficiency)
Note:
(1) A variable modified by register cannot take an address (because the variable has been placed in the register area, and the address is a memory-related concept)
(2) register is only a suggested keyword, suggesting that the computer put the variable into memory, not mandatory, that is, although a variable has been modified by register, but the variable may still be placed in memory.
(3) If you want to use register, please do not use it in large quantities, because the number of registers is limited.
#include int main(){ register int a = 0; printf("&a = %p\n", &a); //compiler error: error 1 error C2103:"&" on register variable //Note that not all compilers report errors here, so far our vs2022 is reporting errors. return 0;}
About "C language auto and register keyword how to use" the content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.