(interview) Summary of keywords in C language __c language

Source: Internet
Author: User
Tags volatile


C language has a lot of keywords, there are register,static,const,extern,typedef, many people may only know its superficial meaning, the use of the understanding of which is not in place, I hope this article can help you:

1.register keywords

Register keyword role: Register as a register variable, in simple terms, the computer is generally composed of CPU, memory, and hard disk, and the CPU is made up of operators, controllers, memory, The role of the Register keyword is to request the compiler to have the variables in the CPU internal registers as much as possible, thus eliminating the time that the CPU crawls data from memory, thereby improving execution efficiency. However, because the internal registers are scarce resources, it is impossible to put all the variables in the register, in general, can be frequently accessed variables (loop variable) in the register.

Use Register to note the following points:

(1), register can only modify local variables, can not modify global variables or functions;

Reason: Global variables are accessed by multiple processes, decorated to register only if the current process knows where to save, other processes are unknown.

(2), can not use & get register variable address

The reason:& gets the memory address, while the register variable saves the address of the Register;

(3), register only modifies the data type which the CPU can accept;

2. Static key word

Static can modify variables, can modify the function, modify the variable, can not only modify the global variable, but also can modify local variables, the local variable to extend the life cycle, until the end of the release of the program, decorated with global variables can only be visible in this file, other files are not visible; Can only be invoked in this file, other files cannot be invoked;

Static modified variables are all stored in the data area of the rest of the data area, if not initialized, the system defaults to initialize to 0;

When to use static adornments.

(1). Want to let a variable after the execution of the program to release, with Static, only in this document visible, the overall risk;

(2). Static modifies the global variable, which can only be used in this file using static, and can be renamed in other files;

(3). Modifier function, play a protective role;

const keyword

1. When the const modifies a variable, the variable is read-only, that is, the variable that is modified by the const, although the space is variable, but the space cannot be modified by the modified variable name.

2, with the role of const modified parameters: To prevent the implementation of the function to modify the value of the actual parameters, once modified, immediately error, easy to modulate.

typedef keywords

Function: Name the data type again;

Its advantages are: 1. Improve the portability of the program;

2. To facilitate the definition of variables, the complex name into a simple name, improve coding efficiency

3. Explain the meaning of certain variables.

volatile keyword

Function: As an instruction keyword, make sure that this instruction is not omitted by the compiler optimization and requires a direct read value each time.

The intent of volatile is variable, and when the value of a variable declared by volatile is required, the system always reads the data back from its memory, even if the previous instruction has just read the data from that point. And the data being read is saved immediately.

Volatile the corresponding variables may be changed in your program itself do not know the circumstances of the change, such as multithreaded programs, common access to the memory, multiple programs can manipulate this variable your own program, It is impossible to determine when this variable will change. Also, for example, he corresponds to a certain state of an external device, and when an external device occurs, the system changes the value of the variable by the driver and interrupt event, and your program does not know.

For the volatile type of variable, each time the system uses it is directly extracted from the corresponding memory, and will not take advantage of the original cache value, in order to adapt to its unknown when the changes will occur, the system will not do to optimize the processing of such variables- Obviously also because its value can change at any time.

Generally speaking, volatile is used in several places as follows:

1, the interruption of the service program for other procedures to detect the variables need to add volatile;

2. The volatile should be added to the signs shared between tasks in a multitasking environment;

3, memory mapping hardware registers are usually added volatile description, because each of its reading and writing may have different meanings;

In addition, these situations often have to consider the integrity of the data (a number of interrelated flags read half interrupted rewrite), in 1 can be implemented through the shutdown, 2 can prohibit task scheduling, 3 of the hardware can only rely on good design.

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.