C language Keywords Auto and register in depth understanding _c language

Source: Internet
Author: User
Tags data structures goto volatile

Keyword Overview
Many friends see here may have the question, often other speak C language books are from HelloWorld, the data type starts the C language to learn, why we should start from the C language keyword? On this point, I have two points to note:
This section is intended for readers who have a basic knowledge of C language (at least you should have studied C language programming in universities and other similar courses).
This chapter combines the author's many years of embedded work, research, teaching experience, from the bottom of the computer hardware to the upper layer of software design mastery, there are a large number of simple and simple examples

In my C language training, it is often from the C language keyword to start, because the C language keyword contains all the words of C language, contains a large number of C language knowledge points, from the C language keyword surgery, first of all you can learn the knowledge to review, secondly, to compare and the author has any different views , cut the crap, let's start with the keyword.
Keywords, also called reserved words, is the compiler can recognize special words, each computer language will have its specific keywords, C language has 32 key words.
Q: Why should there be keywords?
A: The key word is the code must be included in the program design, the compiler in the compilation of C code, it is necessary to divide the C code, the code divided into different parts, these parts are separately parsed and compiled.
int a = 10; int is a keyword, and the compiler sees it appear, and it handles the characters that follow it as an integer variable name.
In other words, the keyword is a special string symbol that the compiler can recognize.
The number of keywords is determined by the compiler, and the keyword case sensitivity is also relevant to the compiler. If the keyword is wrong, then in the parsing of the code, the compiler will report an error: The symbol is not recognized or the symbol cannot be parsed.
Each keyword has a different meaning, which is used to tell the compiler what the programmer is for.

keyword Classification
Each of the 32 keywords has a different meaning, which can be divided into the following categories (underlining the intersection of different classifications) by its meaning:
1) See very much: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 all know and can use, some of which may be rare, even without a single impression: It's also a Ckeywords for language???
1. Stealth Assassin: Auto
Description:
Auto keyword in our written code can hardly see, but it is everywhere, it is so important, but also so aloof, silently fulfilling their obligations, but also incognito.
function:C program is process-oriented, in C code will appear a large number of function modules, each function has its life cycle (also known as the scope), in the function life cycle declared variables are often called local variables, also called automatic variables. For example:

Copy Code code as follows:

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

Copy Code code as follows:

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

Integer variable A is declared within the fun function, its scope is fun function, come out fun function, cannot be referenced, a variable is automatic variable. That is, the compiler will have an int a = 10 plus the keyword of auto.
The presence of auto means that the current variable is scoped to the current function or the local variable of the code snippet, meaning that the current variable is allocated on the memory stack.
Memory Stacks:
If you have learned the data structure, you should know that the stack is the advanced data structures. It is similar to our box packing books, the first throw into the British, the second throw high number, the third throw the novel, then take the book, first take out the first is a novel, the second is high number, the third is British.
Stack operation for the stack and out of the stack, into the stack is to throw a book in the box, out of the stack is taken from the box book. So what does this have to do with our auto variable 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 a reasonable use of memory is a compiler to do a major task. Some variables are one-time-used, such as local variables. Some variables have to be followed by the entire program, such as global variables. To conserve memory space and optimize performance, the compiler usually assigns one-time-used variables to the stack. In other words, the code declares a one-time variable, on the stack on the stack operation. When the variable is used (the end of the lifecycle), the stack operation is done. In this way, when performing different functions, the stack operation is carried out on one of the stacks, which means that they use the same memory space frequently, so that they can use the memory more efficiently.

PS: Some compilers, in order to improve efficiency, do not make the data empty when the stack, which means that the next function of the variable in the stack using the space, the data inside is the result of the last variable operation.
2. Lightning Knife: Register
Description: Register, like its name, rarely appears in the code world, because the variable called the Lightning bolt, Usually occurs only on certain occasions. It is so fast that the CPU is impressed by it, but it has a fatal flaw, its speed "to see the mood" and set, not every time can make people satisfied.
Function: If a variable is a register, it means that the variable will be used as a register variable to make the variable's access speed the fastest. For example: A program logic has a very large loop, there are several variables in the loop to operate frequently, these variables can be declared as register type.
Register variable: register variable refers to a variable directly referencing the register, that is, the result of the operation of the variable name is directly to the register access. Register is the CPU's cronies, CPU operation of each operand and operation results, are temporarily saved by registers, and finally write to memory or read out of memory. That is, the value of the variable is usually kept in memory, and the CPU reads the variable first read the value of the variable from memory into the register, then perform the operation, and then write the result back into memory. Why do you want to design this, instead of directly calculating the value of a variable from memory, and using a register? This is because of the performance of the problem is designed to do so. In a computer system, there are many different types of storage, as shown in table XXX.
table XXX Computer memory category

Name

Speed

Characteristics

Use

Static memory

The fastest

High cost, large volume, suitable for small capacity cache

Registers, caching

Dynamic memory

More quickly

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

Memory


The CPU operates the fastest in the computer, are now up to 3GHZ, and the corresponding memory speed is relatively slow a lot, the fastest access to the register and cache, because of its large size, not suitable for the use of large capacity, so only the two-phase combination of ways to improve efficiency. program code stored in memory, when the use of data, send it to the register, let the CPU to access, use finished, send back memory storage. and C language allows the use of registers to save the value of variables, it is obvious that this can greatly improve the execution speed of the program, but the number of registers is limited, X86 is more than 10, arm up to only 37. It is not possible to declare all variables as register variables, because other code also uses registers, and the register variables we declare are not necessarily stored directly in registers, because the registers may all be occupied by other code. The compiler can only try to arrange for our variables in the register.
When you use a register variable, Please note:
The type to be declared as a register variable should be the type that the CPU registers can accept, meaning that the register variable is a single variable and the length of the variable should be less than or equal to the register length
The address character ' & ' cannot be used on a register variable because the variable does not have a memory address
Use register variables as much as possible in a large number of operations, and the number of variables declared should be as low as possible

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.