Embedded linux engineer interview questions C language basics

Source: Internet
Author: User
Tags strtok
Embedded linux engineers interview questions C language basics-general Linux technology-Linux programming and kernel information, the following is a detailed description. I saw an embedded interview on the Internet and suddenly felt very small ...... In the future, I will post my answers one after another. Of course, some of my answers may not be accurate, and they may not be accurate. While testing and improving myself, hope someone has passed by to give advice.

The following questions must be correctly answered before being scored:
1. How to initialize a character array in C.
2. How to allocate space for an array in C.
3. How to initialize a pointer array.
4. Define an integer pointer array with 10 elements.
5. What is another expression of s [10.
6. Which programming languages are supported in GCC3.2.2.
7. the header file to be included in CHAR_BIT.
8. What is the sum of (-1.2345?
9. How to make local variables have a global life cycle.
10. When should constant strings in C be defined?
11. How to reference the variable of the other party in two. c files.
12. What preparations should be made before using malloc.
13. What should I pay attention to when using the realloc function.
14. What should I pay attention to when using strtok functions.
15. What should I pay attention to when using the gets function.
16. What policies does the C language lexical analysis adopt in terms of length rules?
17. What does a ++ B mean? What's the problem?
18. How to define the values of TRUE and FALSE in the Bool variable.
19. What is the meaning of const in C language. Why do we recommend using const instead of # define when defining constants.
20. What is the meaning of volatile in C language. What is the implication of the compiler when it is used.

This part is some of the questions of ansi c. The premise of the question is that they must all be correct and seem abnormal. But if you think about it, these are the most basic, although we will make such mistakes when using them, the ultimate goal is not to make mistakes, isn't it? Well, it starts from the most basic.



1. How to initialize a character array in C.
This question seems simple, but we need to treat it with the strictest degree of rigor. Key points: initialization, struct, and array. The simplest method is char array [];. This problem seems to have been solved, but there seems to be something missing in Initialization. I personally think: char array [5] = {'1', '2', '3 ', '4', '5'}; or char array [5] = {"12345"}; or char array [2] [10] = {"China ", "Beijing"}; it may be more consistent with the meaning of "initialization.



2. How to allocate space for an array in C.
The simplest method is char array [5], which means to allocate a five-byte space to the array. But we need to know that the array in C is actually a name, and its essence is a pointer, such as char array []; how much space is allocated? Therefore, we need to divide it into two different forms to give the answer:
One is the stack format: char array [5];
One is the heap format: char * array; array = (char *) malloc (5); // C ++: array = new char [5];
I did not understand the meaning of heap and Stack too thoroughly. I understood it again in another day.
We need to understand that the size of space allocation in the first form may be limited by the operating system, for example, windows will be limited to 2 MB, and the second form is flexible in space allocation, if RAM is large enough.



3. How to initialize a pointer array.
First, we define a concept, that is, the pointer to the array and the array that stores the pointer.
Pointer to an array: char (* array) [5]; indicates a pointer to an array containing five characters.
Array for storing pointers: char * array [5]. It indicates that an array contains five pointers pointing to struct-type data.
In my understanding, I Initialize an array containing pointers, char * array [2] = {"China", "Beijing "}; it indicates that an array with two pointers pointing to the processed data is initialized. These two pointers point to the strings "China" and "Beijing" respectively ".



4. Define an integer pointer array with 10 elements.
Since it is just definition rather than initialization, it is very simple and there is no dispute: int * array [10];.



5. What is another expression of s [10.
As mentioned above, arrays and pointers are actually two forms of data existence. If we say that for arrays s [], we know that * s = s [0], another expression of s [10] is: * (s + 10 ).



6. Which programming languages are supported in GCC3.2.2.
This problem is abnormal, just like asking you what the role of # error is. It is undeniable that gcc is the next highlight of linux. It is a compiler highly respected by countless programmers. Its advantages are omitted by 1000 words. If you are interested, you can check it yourself. I flipped through the book and the book said: support C, C ++, Java, Obj-C, Ada, Fortran, Pascal, Modula-3 and other languages, this "wait" is terrible, but I think it is already very complete, if you think it is still incomplete, simply add the ASM, but it is not compiled.



7. the header file to be included in CHAR_BIT.
In combination with the above question, the person answering the question may think that he has crashed. This question is really ...... After searching, it should be limits. h.



8. What is the sum of (-1.2345?
In fact, different integer functions may have different results, but this number is not too controversial. The answer is-1.



9. How to make local variables have a global life cycle.
I think I have to study the concept of life cycle in depth, but this question is relatively simple, that is, static modification is enough, but it is only a longer life cycle, the scope is not extended unless the variable is defined in the static zone of the function in vitro, but it becomes a global variable, as if it does not meet the requirements of the question.



10. When should constant strings in C be defined?
To tell the truth, I do not quite understand the meaning of the question stem. As far as I understand, there are two situations: one is the pre-processing stage, which is defined by # define; and the other is the const modifier, however, const modifies a variable and its meaning is "read-only". It is called a constant that is not accurate, but it can be used as a constant using the method of variable operation. Therefore, the first method is more reliable.



11. How to reference the variable of the other party in two. c files.
This question is also vague. The simplest and most direct method is to add the extern modifier to the variable. Of course, this variable must be a global variable. Another method is to use function calls to indirectly reference variables. For example, a function in this C file references another function in C and passes the variable through real parameters. However, since the question is referenced, it is better to use the first answer.



12. What preparations should be made before using malloc.
In fact, there is a lot of preparation work, for example, you need a computer. Joke: first, we need to know the usage of malloc. Simply put, we need to dynamically allocate a space and return the header pointer of the space. The actual preparation work can be divided into the following points: whether the pointer of this space exists; if it does not exist, define a pointer to be assigned a value, and be clear about the type of pointer to be returned, whether the allocated space is reasonable. If the pointer already exists, you must first determine whether the pointer is NULL before assigning the new space header address to the pointer. If not, otherwise, the original space will be wasted, or an error will occur. After free, you can consider the previous situation.



13. What should I pay attention to when using the realloc function.
I only know about this function, Khan. According to my preliminary understanding, this function is used to re-allocate the space size, and the returned header pointer remains unchanged, only to change the space size. Since it is a change, there will be problems of becoming bigger, smaller, and why. Make sure that the memory overflow is not large. The smaller part of the space will be requisitioned and the original data will no longer exist. Why is it changed? If you want to use it again, let's leave it free first.



14. What should I pay attention to when using strtok functions.
I don't know if I can answer this question comprehensively, because it is rarely used. This function is used to split a string, but the string to be split cannot be a constant. For example, first define a string: char array [] = "part1, part2";, the original form of strtok is char * strtok (char * string, char * delim); we will ", "As the separator, Use pt = strtok (array,", "); first. The result is printed as" part1 ". What about the following is written as pt = strtok (NULL, ","); Note: Use NULL. If the string to be split is divided into N segments, use NULL from the second time. To sum up, you need to note that variables must be used for split strings and separators. Except for the first time you use a pointer to a string, NULL must be used for subsequent operations; note that when using this function, do not lose the pointer, otherwise it will be all messy.



15. What should I pay attention to when using the gets function.
This is a keyboard input function that returns the header address of the input string. When talking about the issues to be aware of, I checked some situations on the Internet first. It should be noted that the gets end with the input of the carriage return. people on this earth know, but many people do not know that, after you input a string, it may still exist in the standard input stream. When you use gets again, it may read the last input, therefore, use fflush (stdin) after use to clear the input stream. Finally, pay attention to the overflow problem. I am vague about this answer. Do you have any high opinions?



16. What policies does the C language lexical analysis adopt in terms of length rules?
I'm speechless ...... Not heard ...... I searched for an article about lexical analyzer at http: // 202.117.80.9/jp2005/20/kcwz/wlkc/03/3 _5_2.htm. Two strategies are mentioned: (1) determine the selected word type based on the longest match principle; (2) if a string can match several word types, the first word type is selected. I don't know whether the question stem is required or what else. I am a programmer. Hope someone else can give me some advice!



17. What does a ++ B mean? What's the problem?
This is actually not a syntax error. According to the classification of operator levels in C, ++ has a higher priority than ++, then this sentence will be viewed by the compiler as: (a ++) + (++ B). Now I understand it. What's the problem? There's no syntax problem, and there's a moral problem! As a good programmer, we need to strive for the legitimacy and readability of the statement. If the person who writes this sentence is in a team, then he will be beaten to death ...... Finally, we will discuss the result: If the value before a is 3 and B is 4, then after this abnormal statement is run, the value of a is 4 and B is 5, the result of the statement is 8.



18. How to define the values of TRUE and FALSE in the Bool variable.
I don't know what traps this question has. I wrote it to the end that the nerves are too big. Generally, we need to define true and FALSE first. Use # define:
# Define TURE 1
# Define FALSE 0
If a variable needs to be defined as bool type, for example, bool a = TURE.



19. What is the meaning of const in C language. Why do we recommend using const instead of # define when defining constants.
First, the answer to this question is a big mouth. The concept of constants seems like I have to read a book ...... As I said, the const modifier can modify a variable to "read-only". Can this be called a constant? I think so. Back to the question, const is read-only. It limits that a variable cannot be changed and no one can change it! Since the variable is modified, the type of the variable can be rich and colorful, int, char, As long as C knows it; but # define won't, there is no type detection mechanism in the pre-processing phase and errors may occur. In addition, the variable can be extern, but # define cannot. It seems that const can save RAM, but I have never verified it. There are many usage and functions of const. I will summarize them and send them later.



20. What is the meaning of volatile in C language. What is the implication of the compiler when it is used.
Finally, the last question. Is it easy ...... If this test is embedded, this question is very important !! In terms of words, volatile is variable-prone. That is to say, some variables may be inexplicably changed while the program is running. In order to save time, the optimizer, sometimes the real value of this variable is not re-read, but the backup in the register is read. In this case, the real value of this variable is "optimized" by the optimizer, in a fashionable word, it is "harmonious. If this modifier is used, it notifies the compiler not to be lazy and read it again honestly! Maybe I am too "plain", so I will refer to the standard explanation of "master:
Volatile is intended to be "changeable ".
Since the speed of accessing registers is faster than that of RAM, the compiler generally reduces the access to external RAM, but may read dirty data. When the value of a variable declared by volatile is required, the system always reads data from its memory, even if the previous command has just read data from it. The read data is saved immediately.
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
Embedded System programmers often deal with hardware, interruptions, RTOS, and so on, all of which require volatile variables. If you do not know volatile content, it will lead to disasters.

Okay, I don't know what the standard answer is. If someone is frowning, never stop. It's a waste of minutes to give me some advice. Thank you very much!
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.