C language keyword auto and register in depth understanding

  • 2020-04-02 01:03:03
  • OfStack

Keyword overview
Many of you may wonder if you read this, because most other C books start with HelloWorld, data types, why do we start with the key words of C? I have two points to make about this:
This chapter is aimed at the readers who have some basic knowledge of C language (at least they should have studied the similar courses of C language programming in university).
This chapter combines the author's many years of embedded work, research, teaching experience, from the computer hardware to the upper layer of the software design through a large number of examples

When I was training for C language, is often from the keywords of the C language, for C language keywords that contains all of the C language vocabulary, include the C language in a large number of knowledge points, key operation from the C language, to review the knowledge before you first, second, to learn and the author have any different opinions, cut the crap, let's begin with the keyword.
Keywords, also known as reserved words, are special words that can be recognized by the compiler. Every computer language has its own specific keyword. In C, there are 32 bits The keyword.
Q: why keyword?
Answer: keyword is the part that the code must contain in the program design, when compiler compiles C code, must carry on the sentence break C code, divide the code into different parts, carry on the analysis and the compilation respectively these parts.
Int a = 10;   Int is the keyword, and the compiler sees it and treats the character after it as an integer variable name.
That is, the keyword is a special string symbol that the compiler can recognize.
The number of keywords is determined by the compiler, and keyword case-sensitivity is also related to the compiler. If a keyword is miswritten, the compiler will report an error during code parsing: symbols are not recognized or cannot be parsed.
Each keyword has a different meaning and is used to inform the compiler programmer of the purpose.

Keyword classification
Each of the 32 keywords has a different meaning, which can be roughly divided into the following categories according to its meaning (the underline indicates the intersection of different categories) :
1) unusual: Auto, register, volatile, goto
2) storage related: Const, extern, register, volatile, static, auto, signed, unsigned
3) data type: Char, short, int, float, long, double, struct, union, enum, void
4) logical control: If, else, for, while, do, break, continue, return, default, switch, case, goto
5) special use: Sizeof, typedef
I believe that most of the keywords we know and can use, and some of them may be rare or even unmemorable: it's also C Language keywords??
1. Stealth assassin: auto
Description:
The auto keyword is almost invisible in the code we write, but it is everywhere, it is so important, it is so aloof from the world, quietly fulfilling their obligations, but incognito.
Function: C programs are process-oriented, and there are a large number of function modules in C code. Each function has its own life cycle (also known as scope). Variables declared in the function life cycle are usually called local variables, also known as automatic variables. Such as:


    int fun(){  
          int a = 10;      // auto int a = 10;  
          // do something  
          return 0;  
    }  

The same code at the page code block index 0
The integer variable a is declared in the fun function, its scope is within the fun function, out of the fun function, cannot be referenced, a variable is an automatic variable. So the compiler is going to have int a = 10 plus the auto keyword.
The emergence of auto means that the scope of the current variable is the local variable of the current function or code segment, which means that the current variable will be allocated on the memory stack.
Memory stack:
If you've ever studied data structures, you should know that a stack is a data structure that comes out first. It's kind of like when we pack a book in a box, the first book goes into britannica, the second book goes into britannica, the third book goes into britannica, so when you pick up the book, you take out the first book, the second book goes into britannica, the second book goes into britannica, the third book goes into britannica.
The operations of the stack are push and push, push is to throw books into the box, and push is to take books from the box. So what does this have to do with our auto allocation space?
Because a program may have a large number of variable declarations, each variable will occupy a certain amount of memory space, and memory space for the computer is a valuable hardware resources, so reasonable use of memory is a compiler to do a major task. Some variables are disposable, such as local variables. Some variables are used throughout the program, such as global variables. To save memory space and optimize performance, compilers typically allocate disposable variables on the stack. That is, a one-time variable is declared in the code and pushed on the stack. When the variable is used up (at the end of the life cycle), the stack operation is performed. In this way, when different functions are executed, they will be on and off the same stack, that is, they are frequently using the same memory space, so that memory can be used more efficiently.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/2013053118090534.gif ">

PS: Some compilers, in order to be more efficient, do not empty the data on the stack, which means that when the variable in the next function is pushed to use the space, the data in it is the result of the last variable operation.
2. Lightning knife: register
Description: register, like its name, rarely appears in the code world because variables that dare to be called lightning knives are usually only available in certain situations. It was so fast that the CPU was impressed, but it had a fatal flaw: its speed was "mood-dependent" and not always satisfying.
Effect: if a variable is registered, it means that the variable will be used as a register variable, making the access to the variable as fast as possible. For example, a program logic has a large loop with several variables that need to be manipulated frequently. These variables can be declared as register types.
Register variable: A register variable refers to a variable that directly references the register, that is, the result of an operation on the variable name is direct access to the register. The register is the trusted member of the CPU. Every operand and the result of the operation of the CPU are temporarily saved by the register before being written to or read from memory. In other words, the value of a variable is usually stored in memory, and the CPU reads the variable by first reading the value of the variable from memory to the register, then performing the operation, and then writing the result back to memory. Why design it this way, instead of directly calculating the value of a variable from memory, and then using a register? This was designed for performance reasons. In computer systems, there are many different types of storage, as shown in table XXX.
Table XXX classification of computer memory

The name of the

speed

The characteristics of

use

Static memory

The fastest

High cost, large size, suitable for small capacity of the cache

Register, cache

Dynamic memory

faster

Low cost, small size, suitable for large easy to save data

memory


In the computer, the CPU operation speed is the fastest, now reached about 3GHZ, and the corresponding memory speed is relatively much slower, access to the fastest registers and cache, due to its large size, not suitable for the use of large capacity, so only the way of the two joint to improve efficiency. Program code is saved in memory, when using the data, it will be sent to the register, let the CPU to access, use up, back to memory. While C language also allows the use of registers to store the value of variables, which obviously can greatly improve the execution speed of the program. However, the number of registers is limited, X86 is more than a dozen, ARM is only 37 at most. It is not possible to declare all the variables as register variables, because other code USES registers, and it is not possible for the declared variables to be stored directly in registers, because they may all be occupied by other code. The compiler can only arrange our variables in registers as much as possible.
When using a register variable, Please note:
To be declared as a register variable type should be the type acceptable to the CPU register, meaning that the register variable is a single variable, and the variable length should be less than or equal to the register length
You cannot use the fetch address character "&" on a register variable because it does not have a memory address
Use register variables for a large number of frequent operations, and the number of declared variables should be as small as possible


Related articles: