[Stick to the top] Chapter 1 C language keywords-auto and register

Source: Internet
Author: User

++ ++

C language deep understanding series-keywords: auto and register

Follow the original author to view other blog posts.

This article is original on this site. You are welcome to repost it! Reprinted please indicate the source:

Http://blog.csdn.net/mr_raptor/article/details/7209452

++ ++

Keyword Overview

Many of my friends may have doubts. Many other C-language books often start from HelloWorld and Data Types in C language. Why should we start from the keywords of C language? I have two points to note:

  • This chapter is intended for readers who have basic knowledge of C language (at least should have studied similar courses such as C language programming in College)
  • This chapter is based on the author's years of embedded work, research and teaching experience, from the underlying computer hardware to the upper layer software design integrated, there are a lot of in-depth examples in the middle

 

When I am training C language, I often start with the keywords of C language, because the keywords of C language contain all the words of C language, including a lot of knowledge points in C language, start with the C language keyword. First, you can review your previous knowledge. Second, you can learn different opinions from the author. Let's start with the keyword.

A keyword, also known as a reserved word, is a special word that the compiler can recognize. Each computer language has a specific keyword. The C language contains 32-bit keywords.

Q: Why is there a keyword?

A: The keyword is a part of the code that must be included in the program design. When the compiler compiles the C code, it must cut the C code into different parts, parse and compile these parts separately.

Int a = 10; int is a keyword. When the compiler sees it, the character after it is processed as an integer variable name.

That is to say, keywords are special character strings recognized by the compiler.

The number of keywords is determined by the compiler, And the keyword case sensitivity is also related to the compiler. If the keyword is incorrect, the compiler reports an error during code parsing: The symbol cannot be recognized or the symbol 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 different meanings. In general, they can be divided into the following categories based on their meanings (underlines indicate that there is an intersection between different categories ):

1) For more information, see:Auto,Register,Volatile,Goto

2) storage problems: const, extern,Register,Volatile, Static,Auto, Signed, unsigned

3) data types: 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 Purposes: sizeof and typedef

I believe that we can understand and use most of the keywords. Some of them may be rare, and even have no idea: they are also the keywords of C language ???

1.Invisible Assassin: auto

Description:The auto keyword is almost invisible in the code we write, but it is everywhere. It is so important and so uncontested. It silently fulfills its obligations, but its name is hidden.

Purpose:C Programs are process-oriented. A large number of function modules will appear in C code. Each function has its lifecycle (also called scope ), variables declared in the function life cycle are usually called local variables or automatic variables. For example:

Int fun () {<br/> int a = 10; // auto int a = 10; <br/> // do something <br/> return 0; <br/>}</p> <p>

Integer variable a is declared in the fun function, and its scope is the fun function. The fun function cannot be referenced. Variable a is an automatic variable. That is to say, the compiler will add the auto keyword before int a = 10.

The emergence of auto means that 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 have learned the data structure, you should know that the stack is an advanced data structure. It is similar to packing a book in a box. The first book is thrown into Da Ying, the second book is pushed to a high number, and the third book is pushed to a novel. When reading a book, The first book is taken out first, the second is high, and the third is Da Ying.

Stack operations are for the inbound and outbound stacks. The inbound stack means to throw books into the box, and the outbound stack means to fetch books from the box. So what does this have to do with the space allocated by our auto variable?

Because a program may have a large number of variable declarations, each variable occupies a certain amount of memory space, and the memory space is a valuable hardware resource for computers, therefore, the rational use of memory is a major task for the compiler. Some variables are used at one time, such as local variables. Some variables must be used along with the entire program, such as global variables. To save memory space and optimize performance, the compiler usually allocates one-time variables to the stack. That is to say, declare a one-time variable in the code, and perform the inbound operation on the stack. When this variable is used up (the lifecycle ends), the output stack operation is performed. In this way, when different functions are executed, the inbound and outbound operations are performed on one stack, that is, they frequently use the same memory space, in this way, the memory can be used more efficiently.

 

PS: Some compilers do not clear data when they exit the stack to improve efficiency. This means that when the variables in the next function use the space in the stack, the data is the result of the last variable operation.

2.Lightning knife: register
Description:
Register is the same as its name and rarely appears in the Code world, because variables that dare to be called the lightning knife usually only appear in some specific scenarios. It is so fast that the CPU is so eye-catching, but it has a fatal drawback. Its speed depends on the mood, not every time it is satisfactory.

Purpose:If a variable is called register, it means that the variable will act as a register variable, making the variable access faster. For example, a program logic has a large loop, and several variables in the loop need to be operated frequently. These variables can be declared as the register type.

Register variable: A register variable refers to a variable that directly references the register, that is, the result of the operation on the variable name is to directly access the register. Registers are the trust of the CPU. each operand and Operation Result of the CPU operation are temporarily saved by the registers before being written to or read from the memory. That is to say, the value of a variable is usually stored in the memory. When the CPU reads the variable, the value of the variable is first read from the memory to the register and then calculated, after the operation, write the result back to the memory. Why is it necessary to design this way, instead of directly performing operations on variable values from the memory, instead of using registers? This is because of performance issues. In computer systems, there are many different types of memory, as shown in table xxx.

Table xxx computer memory categories

Name

Speed

Features

Purpose

Static storage

Fastest

High Cost and large size, suitable for small-capacity caching

Register, Cache

Dynamic Storage

Fast

Low Cost and small size, suitable for big and easy data storage

Memory

In the computer, the CPU operation speed is the fastest, and now it reaches about 3 GHz, while the relative memory speed is much slower. The fastest access speed register and cache, due to its large size, it is not suitable for large-capacity use, so it can only be combined to improve efficiency. The program code is stored in the memory. when data is used, it is sent to the register for the CPU to access. after use, it is sent back to the memory for storage. The C language allows the use of registers to save the value of the variable, which can greatly improve the execution speed of the program. However, the number of registers is limited, and X86 is more than a dozen, up to 37 ARM. We cannot declare all the variables as register variables, because other code also uses registers. Similarly, the declared register variables are not necessarily saved directly in registers, this is because all registers may be occupied by other code. The compiler can only arrange our variables in registers as much as possible.

Note the following when using register variables:

  • To be declared as a register variable type, it 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 "&" for the register variable because the variable does not have a memory address.
  • Use register variables in a large number of frequent operations as much as possible, and the number of declared variables should be as few as possible

 

++ ++

C language deep understanding series-keywords: auto and register

Follow the original author to view other blog posts.

This article is original on this site. You are welcome to repost it! Reprinted please indicate the source:

Http://blog.csdn.net/mr_raptor/article/details/7209452

++ ++

 

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.