C language FAQs

Source: Internet
Author: User
C language FAQ-general Linux technology-Linux programming and kernel information. For more information, see the following section. 1. What is the role of the static keyword?

Few people can answer this simple question completely. In the C language, the keyword static has three obvious functions:
1) In the function body, a variable declared as static remains unchanged when the function is called.
2) in a module (but in the function body), a variable declared as static can be accessed by the function used in the module, but not by other functions outside the module. It is a local global variable.
3) in a module, a function declared as static can only be called by other functions in the module. That is, this function is restricted to use within the local scope of the module that declares it.

2. What does the const keyword mean?

As soon as I hear the subject say, "const means constant", I know that I am dealing with an amateur. Last year, Dan Saks has completely summarized all const usage in his article, So ESP (TRANSLATOR: Embedded Systems Programming) every reader should be very familiar with what const can and cannot do. if you have never read that article, you only need to say that const means "read-only. Although this is not a complete answer, I accept it as a correct answer. (If you want to know more detailed answers, read the Saks Article carefully .)
If the examinee can answer this question correctly, I will ask him an additional question:
What does the following statement mean?
Const int;
Int const;
Const int *;
Int * const;
Int const * a const;

/******/
The first two functions are the same. a is a constant integer. Third, it means that a is a pointer to a constant INTEGER (that is, an integer cannot be modified, but a pointer can be ). Fourth, a is a constant pointer to an integer (that is, the integer to which the Pointer Points can be modified, but the pointer cannot be modified ). The last one means that a is a constant pointer to a constant INTEGER (that is, the pointer to an integer cannot be modified, and the pointer cannot be modified ). If the examinee can answer these questions correctly, he will leave a good impression on me. By the way, you may ask, even if you don't need the keyword const, it is still easy to write a program with the correct function. Why should I pay so much attention to the keyword const? I also have the following reasons:
1) The function of the keyword const is to send very useful information to the person who reads your code. In fact, the purpose of declaring a parameter as a constant is to inform the user of the application of this parameter. If you spend a lot of time cleaning up the garbage left by others, you will soon learn to thank this excess information. (Of course, programmers who know how to use const seldom leave garbage for others to clean up .)
2) by attaching some information to the optimizer, using the keyword const may produce more compact code.
3) the rational use of the keyword const can enable the compiler to naturally protect those parameters that do not want to be changed, so as to prevent them from being accidentally modified by code. In short, this can reduce the occurrence of bugs.

3. What does the keyword volatile mean? Three different examples are provided.

A variable defined as volatile means that this variable may be unexpectedly changed, so that the compiler will not assume the value of this variable. Precisely, the optimizer must carefully re-read the value of this variable every time when using this variable, rather than using the backup stored in the register. The following are examples of volatile variables:
1) Hardware registers of parallel devices (for example, Status Registers)
2) Non-automatic variables that will be accessed in an interrupt service subroutine)
3) variables shared by several tasks in multi-threaded applications

People who cannot answer this question will not be hired. I think this is the most basic problem to distinguish between C programmers and embedded system programmers. Embedded people often deal with hardware, interruptions, RTOS, etc. All of these require volatile variables. If you do not know volatile content, it will lead to disasters.
If the subject correctly answers this question (well, I suspect it will be the case), I will go a little deeper to see if this guy understands the full importance of volatile.
1) can a parameter be const or volatile? Explain why.
2) can a pointer be volatile? Explain why.
3); what are the following function errors:

Int square (volatile int * ptr)
{
Return * ptr ** ptr;
}

The answer is as follows:
1) Yes. One example is read-only status registers. It is volatile because it may be unexpectedly changed. It is const because the program should not try to modify it.
2); yes. Although this is not very common. One example is when a service subroutine repairs a pointer to a buffer.
3) This code is a little abnormal. The purpose of this Code is to return the pointer * ptr points to the square of the value. However, since * ptr points to a volatile parameter, the compiler will generate code similar to the following:

Int square (volatile int * ptr)
{
Int a, B;
A = * ptr;
B = * ptr;
Return a * B;
}

* The value of * ptr may be unexpectedly changed, so a and B may be different. As a result, this Code may not return the expected square value! The correct code is as follows:

Long square (volatile int * ptr)
{
Int;
A = * ptr;
Return a *;
}

4. What is the output of the following code? Why?

Void foo (void)
{
Unsigned int a = 6;
Int B =-20;
(A + B> 6 )? Puts ("> 6"): puts ("<= 6 ");
}
This problem tests whether you understand the principle of automatic Integer Conversion in the C language. I found that some developers know very little about these things. In any case, the answer to this unsigned integer is "> 6 ". The reason is that when there are signed and unsigned types in the expression, all operands are automatically converted to the unsigned type. Therefore,-20 is a very large positive integer, so the result calculated by this expression is greater than 6. This is very important for embedded systems that should frequently use the unsigned data type. If you fail to answer this question, you will not be able to get the job.

5 Typedef is frequently used in C to declare an existing data type. You can also use a pre-processor to do similar things. For example, consider the following example:

# Define dPS struct s *
Typedef struct s * tPS;

The intention of the above two cases is to define the dPS and tPS as a pointing structure s pointer. Which method is better? (If so) Why? This is a very subtle question. Anyone should be congratulated on answering this question (the legitimate reason. The answer is: typedef is better. Consider the following example:

DPS p1, p2;
TPS p3, p4;

The first extension is

Struct s * p1, p2;
.
The code above defines p1 as a point to the structure, and p2 as an actual structure, which may not be what you want. The second example correctly defines the p3 and p4 pointers.

6. What is the difference between a variable and a variable definition?

The use of the keyword const has two advantages. First, if the compiler knows that the value of a variable will not change, the compilation process. the program can be optimized in order; second, the Compilation Program will try to ensure that the value of the variable will not be changed due to the programmer's negligence.
Of course, using # define to define constants also has the same benefits. The reason for using const instead of # define to define constants is that the const variable can be of any type (for example, a structure, but a constant defined by # define cannot represent a structure ). In addition, the const variable is a real variable and has available addresses, this address is unique (Some compilers generate a new copy each time they use a string defined by # define, as shown in Figure 9.9 ).

7 can the static variable be described in the header file?

If a static variable is defined, it must be defined in the same file (because the storage type modifiers static and extern are mutually exclusive ). You can define a static variable in the header file, but this will make the source files containing the header file get a private copy of the variable, which is generally not the result you want.

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.