The most comprehensive questions for C/C ++ interviews (1)

Source: Internet
Author: User
Embedded Development. C language interview questions

Preprocessor)

1. Use the preprocessing command # define to declare a constant to indicate the number of seconds in a year (ignore the leap year problem)
# Define seconds_per_year (60*60*24*365) UL
Here I want to see several things:
1). # basic knowledge of define syntax (for example, it cannot end with a semicolon, use of parentheses, and so on)
2 ). knowing that the pre-processor will calculate the value of a constant expression for you, therefore, it is clearer and easy to directly write how much seconds you calculate in a year rather than calculate the actual value.
3) realize that this expression will overflow the integer number of a 16-bit machine-so the long integer sign L is used to tell the compiler that this constant is the long integer number.
4). If you use ul in your expression to represent an unsigned long integer, you have a good starting point. Remember, the first impression is very important.

2. Write a "standard" macro min, which inputs two parameters and returns a smaller one.
# Define min (a, B) (a <= B? A: B)

This test is intended for the following purposes:

1). identifier # basic knowledge of define application in macros. This is important, because until the inline operator becomes part of standard C, macros are the only way to facilitate the generation of embedded code. For embedded systems, in order to achieve the required performance, embedded code is often a required method.
2) knowledge of triple-condition operators. The reason for the existence of this operator in C language is that it enables the compiler to produce code that is more optimized than if-then-else. It is important to understand this usage.
3). Be careful to enclose parameters in brackets in macros.
4). I also use this question to discuss the side effects of macros. For example, what will happen when you write down the code below?
Least = min (* P ++, B );

3. pre-processor ID # What is the purpose of error?

If you do not know the answer, see References 1. This problem is useful for distinguishing a normal guy from a nerd. Only the nerd can read the appendix of the C language textbook to find out
The answer to the question. Of course, if you are not looking for a nerd, you 'd better ask yourself not to know the answer.

Infinite Loops)

4. Infinite loops are often used in embedded systems. How do you write an infinite loop in C?

There are several solutions to this problem. My preferred solution is:
While (1 ){}
Some programmers prefer the following solutions:
For (;;){}
This implementation method makes me embarrassed because this syntax does not exactly express what is going on. If a candidate provides this solution, I will use this as an opportunity to explore what they do.
Basic principle. If their basic answer is: "I was taught to do this, but I never thought of why ." This leaves a bad impression on me.
The third solution is to use GOTO
Loop:
...
Goto loop;
If the candidate gives the above solution, it indicates that he is an assembly language programmer (which may be a good thing) or a basic/Fortran programmer who wants to enter a new field.

Data declarations)

5. Use variable A to give the following definition
A) An integer)
B) a pointer to an integer (a pointer to an integer)
C) a pointer to a pointer pointing to an integer (a pointer to an integer)
D) an array of 10 integers (an array of 10 integers)
E) an array with 10 pointers pointing to an integer (an array of 10 pointers to integers)
F) A pointer to an array with 10 integers (a pointer to an array of 10 integers)
G) a pointer to a function. The function has an integer parameter and returns an integer number (a pointer to a function that takes an integer as an argument and returns an integer)
H) An array with 10 pointers pointing to a function, this function has an integer parameter and returns an integer (an array of ten pointers to functions that 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 an integer
D) int A [10]; // an array of 10 Integers
E) int * A [10]; // an array of 10 pointers to Integers
F) int (* A) [10]; // a pointer to an array of 10 Integers
G) int (* A) (INT); // a pointer to a function a that takes an integer argument and returns an integer
H) int (* A [10]) (INT); // an array of 10 pointers to functions that take an integer argument and return an integer

People often claim that there are several questions that can be answered only after a book is opened. I agree with this statement. When I wrote this article, I checked the book to confirm the correctness of the syntax. But when I was interviewed, I expected to be asked this question (or similar questions ). Because I knew the answer to this question during the interview period. If the examinee does not know all the answers (or at least most of the answers), he will not be prepared for the interview. If the interviewer is not prepared for the interview, why should he prepare?

Static

6. 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 obvious functions:
1). In the function body, a variable declared as static remains unchanged during the function call process.
2) within a module (but in the external body of the function), 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.
Most of the respondents can answer part 1 correctly. Some of them can answer part 2 correctly. Few people can understand part 3. This is a serious disadvantage of a candidate because he obviously does not understand the benefits and importance of localized data and code scope.

Const

7. What is the meaning of the keyword const?
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 keyword const is used to send very useful information to people who read 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 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 parameters that do not want to be changed and prevent them from being modified by unintentional code. In short, this can reduce the occurrence of bugs.

Volatile

8. What is the meaning of the keyword volatile and three different examples are given.

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 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.
If the subject correctly answers this question (well, I doubt this will happen), 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 has a prank. 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 *;
}

Bit manipulation)

9. In an embedded system, you must perform bit operations on variables or registers. Given an integer variable A, write two pieces of code. The first one sets bit 3 of A, and the second one clears bit 3 of. In the preceding two operations, you must keep the other bits unchanged.

There are three basic responses to this problem.
1). I don't know how to start. The quilt has never worked on any embedded system.
2). Use bit fields. Bit fields is thrown into the dead corner of C language. It ensures that your Code cannot be transplanted between different compilers, and that your Code cannot be reused. Recently, unfortunately, I have seen infineon write drivers for its more complex communication chip. It uses bit fields and is completely useless to me, because my compiler uses other methods to implement bit fields. Morality: Never let a non-embedded guy stick the actual hardware edge.
3) Use # defines and bit masks. This is a highly portable method and should be used. The best solution is as follows:
# Define bit3 (0x1 <3)
Static int;
Void set_bit3 (void)
{
A | = bit3;
}
Void clear_bit3 (void)
{
A & = ~ Bit3;
}
Some people like to define a mask to set and clear values and define some descriptive constants at the same time, which is acceptable. I want to see several key points: constant, | = and & = ~ Operation.

Accessing fixed memory locations)

10. embedded systems often require programmers to access a specific memory location. In a project, the value of an integer variable whose absolute address is 0x67a9 must be set to 0xaa66. The compiler is a pure ANSI compiler. Write code to complete this task.

Test whether you know that it is legal to forcibly convert an integer number (typecast) into a pointer to access an absolute address. The implementation of this problem varies with the individual style. The typical code is as follows:
Int * PTR;
PTR = (int *) 0x67a9;
* PTR = 0xaa55;

A relatively obscure method is:
* (Int * const) (0x67a9) = 0xaa55;

Even if your taste is closer to the second option, I suggest you use the first option during the interview.

Interrupts)

11. interruption is an important part of embedded systems, which causes many compilation developers to provide an extension to interrupt Standard C support. It indicates that a new keyword _ interrupt is generated. The following code uses the _ interrupt keyword to define an interrupt service subroutine (ISR). Please comment on this code.

_ Interrupt double compute_area (double radius)
{
Double area = pI * radius;
Printf ("Area = % F", area );
Return area;
}

There are too many errors in this function, so people don't know where to start:
1). ISR cannot return a value. If you do not understand this, you will not be hired.
2). ISR cannot pass parameters. If you do not see this, your chances of being hired are equivalent to the first.
3). In many processors/compilers, floating points are generally not reentrant. Some processors/compilers need to import the registers at the amount into the stack. Some processors/compilers do not allow floating-point operations in ISR. In addition, ISR should be short and efficient, and it is unwise to perform floating point operations in ISR.
4). With the 3.1 pulse, printf () often has re-import and performance problems. If you lose the third and fourth points, I won't be too embarrassed. Needless to say, if you can get the last two points, your prospects for employment will become brighter.

Code example)

12. 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 ");
}

To test whether you understand the principle of automatic Integer Conversion in 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.

13. Evaluate the following code snippet:

Unsigned int zero = 0;
Unsigned int compzero = 0 xFFFF;
/* 1's complement of zero */

For a 16-bit processor of the int type, the above Code is incorrect. It should be written as follows:

Unsigned int compzero = ~ 0;

This question reveals whether the examinee understands the importance of the processor font length. In my experience, good embedded programmers can understand hardware details and its limitations very accurately. However, PC programs often treat hardware as an unavoidable headache.
At this stage, the examinee is either totally frustrated or confident. If it is clear that the candidate is not good, the test will end here. However, if the candidates are doing well, I will throw the following append questions. These questions are difficult. I think only very good candidates can do well. To raise these questions, I hope to see more methods for the examinee to cope with the questions, rather than the answers. Whatever it is, you should be entertaining...

Dynamic Memory Allocation (dynamic memory allocation)

14. Although not as common as embedded computers, embedded systems still have the process of dynamically allocating memory from heap. In the embedded system, what are the possible problems with dynamic memory allocation?

In this case, I expect the examinee to mention memory fragmentation, fragment collection issues, variable holding time, and so on. This topic has been widely discussed in the ESP magazine (mainly P. j. plauger, his explanation is far more than anything I can mention here). Let's look back at these magazines! After putting the examinee into a false sense of security, I come up with such a small program: What is the output of the following code snippets? Why?

Char * PTR;
If (PTR = (char *) malloc (0) = NULL)
Puts ("got a null pointer ");
Else
Puts ("got a valid Pointer ");

This is an interesting question. Recently, one of my colleagues inadvertently passed the 0 value to the malloc function. After obtaining a valid pointer, I came up with this question. This is the above Code. The output of this code is "got a valid Pointer ". I used this to discuss such a problem and see if the subject thought of the library routine. It is important to get the correct answer, but the solution to the problem and the basic principle of your decision are more important.

Typedef

15. 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.

Obscure syntax

16. The C language agrees to some shocking structures. Is the following structure legal? If so, what does it do?
Int A = 5, B = 7, C;
C = A ++ B;

This question will end happily in this quiz. Whether you believe it or not, the above example is completely syntactic. The problem is how does the compiler handle it? The authors of low-level compilation will actually argue about this issue. According to the best processing principle, the compiler should be able to handle as many valid usage as possible. Therefore, the above Code is processed:
C = A ++ B;
Therefore, a = 6, B = 7, c = 12 after the code is executed.
If you know the answer or guess the correct answer, you can do well. If you do not know the answer, I will not regard it as a question. I found that the biggest benefit of this problem is: this is a good topic about code writing style, code readability, and code modification.

What will print out?

Main ()
{
Char * P1 = "name ";
Char * P2;
P2 = (char *) malloc (20 );
Memset (P2, 0, 20 );
While (* P2 ++ = * P1 ++ );
Printf ("% Sn", P2 );

}

Answer: empty string.

What will be printed as the result of the Operation below:

Main ()
{
Int x = 20, y = 35;
X = y ++ x ++;
Y = ++ y ++ X;
Printf ("% d % DN", x, y );
}

Answer: 5794

What will be printed as the result of the Operation below:

Main ()
{
Int x = 5;
Printf ("% d, % d, % DN", x, x <2, x> 2 );
}

Answer: 5, 20, 1

What will be printed as the result of the Operation below:

# Define swap (A, B) A = a + B; B = A-B; A = A-B;
Void main ()
{
Int x = 5, y = 10;
Swap (x, y );
Printf ("% d % DN", x, y );
Swap2 (x, y );
Printf ("% d % DN", x, y );
}

Int swap2 (int A, int B)
{
Int temp;
Temp =;
B =;
A = temp;
Return 0;

}

Answer: 10, 5
10, 5

What will be printed as the result of the Operation below:

Main ()
{
Char * PTR = "Cisco Systems ";
* PTR ++; printf ("% Sn", PTR );
PTR ++;
Printf ("% Sn", PTR );
}

Answer: Cisco Systems
ISCO Systems

What will be printed as the result of the Operation below:

Main ()
{
Char S1 [] = "Cisco ";
Char S2 [] = "systems ";
Printf ("% s", S1 );
}
Answer: Cisco

What will be printed as the result of the Operation below:

Main ()
{
Char * P1;
Char * P2;
P1 = (char *) malloc (25 );
P2 = (char *) malloc (25 );

Strcpy (P1, "Cisco ");
Strcpy (P2, "systems ");
Strcat (P1, P2 );

Printf ("% s", P1 );

}

Answer: ciscosystems

The following variable is available in file1.c, who can access it? :

Static int average;

Answer: all the functions in the file1.c can access the variable.

What will be the result of the following code?

# Define true 0 // some code
While (true)
{

// Some code

}

Answer: This will not go into the loop as true is defined as 0.

What will be printed as the result of the Operation below:

Int X;
Int modifyvalue ()
{
Return (x + = 10 );
}
Int changevalue (int x)
{
Return (x + = 1 );
}

Void main ()
{
Int x = 10;
X ++;
Changevalue (X );
X ++;
Modifyvalue ();
Printf ("first output: % DN", X );

X ++;
Changevalue (X );
Printf ("second output: % DN", X );
Modifyvalue ();
Printf ("third output: % DN", X );

}

Answer: 12, 13, 13

What will be printed as the result of the Operation below:

Main ()
{
Int x = 10, y = 15;
X = x ++;
Y = ++ y;
Printf ("% d % DN", x, y );
}

Answer: 11, 16

What will be printed as the result of the Operation below:

Main ()
{
Int A = 0;
If (A = 0)
Printf ("Cisco systemsn ");
Printf ("Cisco systemsn ");
}

Answer: two lines with "Cisco Systems" will be printed.

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.