Embedded development test Questions for C + +

Source: Internet
Author: User

C + + embedded development surface question preprocessor (preprocessor) 1. Declare a constant with pre-processing directive # # to indicate how many seconds in 1 years (ignoring leap year issues) #define SECONDS_PER_YEAR (* * * * 365) UL I want to see a few things here: 1). Basic knowledge of #define syntax (e.g., cannot end with semicolons, use of parentheses, etc.) 2). Knowing that the preprocessor will calculate the value of a constant expression for you, it is clearer and less expensive to write directly how many seconds in a year instead of calculating the actual value. 3). Realize that this expression will overflow the integer number of a 16-bit machine-so use the long integer symbol l to tell the compiler that this constant is a long integer number. 4). If you are using UL (for unsigned long integers) in your expression, then you have a good starting point. Remember, first impressions are important. 2. Write a "standard" macro min, this macro enters two parameters and returns the smaller one. #define MIN (A) (A) <= (B)? (A): (B) cc++ question set cc++ development This test is for the purposes set out below: 1). Identifies the basic knowledge that a # define is applied in a macro. This is important because, until the embedding (inline) operator becomes part of standard C, macros are the only way to generate embedded code, and embedding code is often a necessary method for embedded systems to achieve the required performance. 2). Knowledge of the triple condition operator. The reason that this operator exists in the C language is that it allows the compiler to produce code that is more optimized than if-then-else, and it is important to understand this usage. 3). Learn to carefully enclose the parameters in parentheses in a macro 4). I also use this issue to start discussing the side effects of macros, such as: What happens when you write the following code? Least = MIN (*p++, b); Macro definition # define MIN (A) (A) <= (B)? (A): (b) The result of the effect on min (*p++, B) was: ((*p++) <= (b)? (*p++): (*p++)) This expression can have side effects, and pointer p will do three + + self-increment operations. 3. What is the purpose of the preprocessor identity #error? If you don't know the answer, see reference 1. This question is useful for distinguishing between a normal guy and a nerd. Only nerds can read the appendix to the C language textbook to find answers like this. Of course, if you're not looking for a nerd, it's best for a candidate to want to knowAnswer to the Tao.    Data declarations 4. Use variable A to give the following definition a) an integer number (an integer) b) A pointer to the integer number (a pointer to an integer) c) A pointer to the pointer that points to the number of integers (a pointer to a pointe  R to a integer) d) An array of 10 integers (an arrays of integers) e) An array of 10 pointers that point to an integer number (an array of ten pointers to integers) f) A pointer to a 10 integer array (a pointer to an array of integers) g) A pointer to the function that has an integer parameter and returns an integer number (a pointer to a function that TA Kes An integer as a argument and returns an integer) h) A 10-pointer array that points to a function that has an integer parameter and returns an integer number (an array of ten pointer s to functions this take an integer argument and return an integer) The answer is: a) int A; An integer b) int *a; A pointer to an integer c) int **a; A pointer to a pointer to an integer d) int a[10]; An array of ten integers e) int *a[10]; An array of ten pointers to integers f) int (*a) [10]; A pointer to an array of integers g) int (*a) (int); A pointer to a function A, takes an integer argument and returns an integer h) int (*a[10]) (int); An array ofPointers to functions that takes an integer argument and return an integer people often claim that there are a few questions here that have to be answered by flipping the book, and I agree. When I wrote this article, I did check the book in order to determine the correctness of the grammar. But when I was interviewed, I expected to be asked the question (or similar question). Because during the interview, I was sure I knew the answer to the question. If the candidate does not know all the answers (or at least most of them), then he is not prepared for the interview, and if the interviewer does not prepare for the interview, then why should he be prepared? 5. What is the role of the keyword static? Few people can answer this simple question completely. In the C language, the keyword static has three distinct effects: 1). In the function body, a variable declared as static maintains its value in the process of the function being called. 2). Within the module (but outside the function body), a variable declared as static can be accessed by the function used within the module, but not by other functions outside the module. It is a local global variable. 3). Within the module, a function declared as static can only be called by other functions within this module. That is, this function is restricted to the local scope of the module that declares it. Most candidates are able to answer the first part correctly, and part of them can answer the second part correctly, with very few people able to understand the third part. This is a serious drawback for a candidate because he clearly does not understand the benefits and importance of localizing data and code scope. 6. What is the meaning of the keyword const? As soon as I heard the interviewee say "Const means constant", I knew I was dealing with an amateur. Last year Dan Saks has completely summed up all the usages of const in his article, so ESP (translator: Embedded Systems programming) Every reader should be very familiar with what const can and cannot do. If you've never read that article, you can just say that const means "read-only". Although this answer is not a complete answer, I accept it as a correct answer. (If you want to know more about the answer, read Saks's article carefully.) If the candidate is able to answer the question correctly, I will ask him an additional question: what does the following statement mean? Const int A; int const A; const int *a; int * const A; int const * a const; The first two functions are the same, a is a constant integer number. The third means that a is a pointer to a constant integer number.The pointer (that is, the integer number is non-modifiable, but the pointer can). The fourth meaning a is a constant pointer to an integer number (that is, the integer number pointed to by the pointer can be modified, but the pointer is non-modifiable). The last one means that a is a constant pointer to a constant number (that is, the integer number pointed to by the pointer is not modifiable and the pointer is not modifiable). If the candidate can answer these questions correctly, then he leaves a good impression on me. Incidentally, perhaps you may ask, even without the keyword const, it is easy to write the function of the correct program, then why do I value the keyword const? I also have the following several reasons: 1). The function of the keyword const is to convey very useful information to the person who is reading your code, and in fact, declaring a parameter as a constant is to tell the user the purpose of the parameter's application. If you have spent a lot of time cleaning up the rubbish left by others, you will soon learn to thank for the extra information. (Of course, it's very rare for a const programmer to leave rubbish for others to clean up.) ) 2). By giving some additional information to the optimizer, using the keyword const may produce more compact code. 3). The use of the keyword const can make it natural for the compiler to protect those parameters that you do not want to change, and to prevent them from being unintentionally modified by the code. In short, this can reduce the occurrence of bugs. 7. What is the meaning of the keyword volatile and gives three different examples. A variable that is defined as volatile means that the variable may be unexpectedly changed so that the compiler does not assume the value of the variable. Precisely, the optimizer must carefully re-read the value of the variable each time it uses the variable, rather than using the backup stored in the register. Here are a few examples of volatile variables: 1). Hardware registers for parallel devices (e.g., status registers) 2). A non-automatic variable (non-automatic variables) 3) that is accessed in an interrupt service subroutine. A variable that is shared by several tasks in multi-threaded applications The person who cannot answer the question will not be hired. I think this is the most basic problem of distinguishing between C programmers and embedded system programmers. Embedded system programmers often deal with hardware, interrupts, RTOs, and so on, which require volatile variables. Not knowing volatile content will bring disaster. Assuming that the interviewee answered the question correctly (well, wondering if this would be the case), I'll look at it a little bit and see if this guy knows exactly the importance of volatile. 1). Can a parameter be either const or volatile? explain why. 2). Can a pointer be volatile? explain why. 3). What is wrong with the following function: int square (volatile int *ptr) {return *ptr * *PTR;} Below is the answer: 1). Yes. An example is a read-only status register. It is volatile because it can be changed unexpectedly. It is const because the program should not attempt to modify it. 2). Yes. Although this is not very common. An example is when a service subroutine fixes a pointer that points to a buffer. 3). There's a prank on this piece of code.        The purpose of this code is to return the pointer *ptr to the square of the value, but since *ptr points to a volatile parameter, the compiler will produce code similar to the following: int square (volatile int *ptr) {int A, B;        A = *ptr;        b = *ptr; return a * b; Because the value of *ptr can be unexpectedly changed, A and B may be different. As a result, this code may return to the square value you expect!        The correct code is as follows: Long square (volatile int *ptr) {int A;        A = *ptr; Return a * A;   }

  

Embedded development test Questions for C + +

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.