C language interview questions-Huawei article 2 (full answer)

Source: Internet
Author: User

Huawei (full answer)

(1) What is pre-compilation and when pre-compilation is required:
Answer:
1. Always use a large scale that is not changed frequently Code Body.
2, Program Consists of multiple modules. All modules use a set of standard inclusion files and the same compilation options. In this case, all contained files can be precompiled into a precompiled header.
(2) What are the differences between char * const P char const * P const char * P and the above three?
Answer:
Char * const P; // constant pointer. The value of P cannot be modified.
Char const * P; // pointer to a constant. The constant value cannot be changed to const char * P; // and char const * P
(3) Char str1 [] = "ABC"; char str2 [] = "ABC"; const char str3 [] = "ABC "; const char str4 [] = "ABC"; const char * str5 = "ABC"; const char * str6 = "ABC"; char * str7 = "ABC "; char * str8 = "ABC"; cout <(str1 = str2) <Endl; cout <(str3 = str4) <Endl; cout <(str5 = str6) <Endl; cout <(str7 = str8) <Endl;
The result is: 0 0 1 1 str1, str2, str3, and str4 are array variables with their own memory space. str5, str6, str7, and str8 are pointers, they point to the same constant area.
(4) Are there any problems with the usage of the two sizeof in the following code?
[C easy] void uppercase (char STR []) // converts lowercase letters in STR into uppercase letters {for (size_t I = 0; I <sizeof (STR) /sizeof (STR [0]); ++ I) if (a <= STR [I] & STR [I] <= z) STR [I]-= (a-A);} Char STR [] = "ABCDE"; cout <"str:" <sizeof (STR) /sizeof (STR [0]) <Endl; uppercase (STR); cout <STR <Endl; answer: there is a problem with sizeof in the function. According to the syntax, sizeof, for example, is used as an array, can only measure the size of a static array, and cannot detect the dynamically allocated or external array size. STR outside the function is a static-defined array, so its size is 6, because there is \ 0, STR in the function is actually a pointer to the string, there is no additional array-related information, so sizeof acts on it and only looks at it as a pointer. A pointer is 4 bytes, SO 4 is returned.
(5) How many digits is the pointer of a 32-bit machine:
You only need to check the number of bits in the address bus. Machines later than 80386 have 32 data buses. Therefore, the number of digits of the pointer is 4 bytes.
6. Main () {int A [5] = {1, 2, 3, 4, 5}; int * PTR = (int *) (& A + 1); printf ("% d, % d ", * (a + 1), * (ptr-1);} answer: 2. 5 * (a + 1) is a [1], * (ptr-1) is a [4], the execution result is 2, 5 & A + 1 is not the first address + 1, the system will consider that the offset of an array a is the offset of an array (in this example, it is five int values) int * PTR = (int *) (& A + 1 ); the PTR is actually & (A [5]), that is, the reason for a + 5 is as follows: & A is an array pointer and its type is int (*) [5]; the pointer plus 1 should add a certain value according to the pointer type. for different types of pointers + 1, the increase in size is different. A is an int array pointer with a length of 5, therefore, we need to add 5 * sizeof (INT). Therefore, PTR is actually a [5], but the PRT and (& A + 1) types are different (this is important) so the prt-1 will only subtract sizeof (int *) A, & A address is the same, but the meaning is not the same, a is the first address of the array, that is, a [0] address, & A is the first address of the object (array), and a + 1 is the address of the next element of the array, that is, a [1]. & A + 1 is the address of the next object, that is, a [5].
7. What is the problem with the following code: int main () {char a; char * STR = & A; strcpy (STR, "hello"); printf (STR); Return 0 ;} answer: If no memory space is allocated for STR, an exception occurs. The problem is that you copy a string into the address indicated by the character variable pointer. Although the results can be correctly output, the program crashes because internal read/write is performed out of the border.
8. Char * s = "AAA"; printf ("% s", S); s [0] = B; printf ("% s", S); what is wrong? Answer: "AAA" is a String constant. S is a pointer pointing to this string constant, so there is a problem when declaring S. Cosnt char * s = "AAA"; then, because it is a constant, it is illegal to assign values to s [0.
9. Write a "standard" macro. This macro inputs two parameters and returns a smaller one. Answer:. # define min (x, y) (x)> (y )? (Y) :( X) // No ';' at the end ';'
10. Infinite loops are often used in embedded systems. How do you use C to write an infinite loop. Answer: While (1) {} or (;;)
11. What is the role of the static keyword? Answer: define static variables.
12. What does the const keyword mean? Answer: variables that cannot be modified by constants.
13. What does the keyword volatile mean? And give three different examples? Answer: It is prompted that the value of the compiler object may change without being monitored by the compiler.
14. What does int (* s [10]) (INT) represent? Answer: int (* s [10]) (INT) function pointer array. Each Pointer Points to an int func (int param) function.
15. There are the following expressions: int A = 248; B = 4; int const c = 21; const int * D = & A; int * const E = & B; int const * f const = & A; which of the following expressions will be banned by the compiler? Why? Answer: * c = 32; D = & B; * D = 43; E = 34; E = & A; F = 0x321f; * c what is this, * d Indicates const, and E = & A indicates that const prohibits const * f const = &;
16. Exchange the values of two variables without the third variable. That is, a = 3, B = 5, after the exchange a = 5, B = 3; answer: There are two solutions, one is to use Arithmetic Algorithm , Which can be ^ (exclusive or) A = a + B; B = A-B; A = A-B; or A = a ^ B; // only int, char .. B = a ^ B; A = a ^ B; or a ^ = B ^ =;
17. What is the difference between struct in C and C ++? Answer: The main difference between struct in C and C ++ is that struct in C cannot contain member functions, while struct in C ++ can. In C ++, the main difference between struct and class is that the default access permission is different. struct is public by default, while class is private by default.
18. # include <stdio. h> # include <stdlib. h> void getmemory (char * P) {P = (char *) malloc (100); strcpy (P, "Hello World");} int main () {char * STR = NULL; getmemory (STR); printf ("% s/n", STR); free (STR); Return 0;} answer: Program crash, the malloc in getmemory cannot return dynamic memory. Free () is dangerous for STR operations.
19. Char szstr [10]; strcpy (szstr, "0123456789"); what is the result? Why? Answer: Invalid OS may be caused by different lengths.
20. List the synchronization mechanisms of several processes and compare their advantages and disadvantages. Answer: The atomic operation semaphore mechanism is used to spin the lock pipe process. It is a distributed system.
21. Communication between processes answer: shared storage system message transmission system pipeline: Based on the file system
22. Cause of process deadlock answer: illegal resource competition and process Promotion order
23. The answer to the four necessary conditions for deadlock: mutex, request persistence, non-deprivation, loop
24. deadlock handling answer: ostrich policy, prevention policy, avoidance policy, detection and deadlock Removal
25. What are the Process Scheduling Policies in the operating system? Answer: FCFS (service first), priority, time slice rotation, multi-level feedback
26. What is the difference between static and non-static members of a class? Answer: each class has only one static member, and each object has one non-static member.
27. How are pure virtual functions defined? What should I pay attention to when using it? Answer: Virtual void F () = 0; it is an interface and must be implemented as a subclass.
28. Differences between arrays and linked lists answer: array: sequential data storage, fixed-size connected tables: data can be stored randomly, and the size can be dynamically changed
29. What is the layer-7 model of ISO? What layer does TCP/UDP belong? What are the advantages and disadvantages of TCP/UDP? Answer: The application layer presentation layer Session Layer transport layer Network Layer Physical Layer TCP/UDP belongs to the transport layer TCP Service provides data stream transmission, reliability, effective flow control, full duplex operations and multiplexing technology. Unlike TCP, UDP does not provide reliable IP protocol mechanisms, stream control, and error recovery functions. Because UDP is relatively simple, the UDP header contains a few bytes, which consumes less than the TCP load. TCP: provides stable transmission services with traffic control. The disadvantage is that the packet header is large and the redundancy is poor. UDP: it does not provide stable services, with a small packet header and low overhead.
30: (void *) Does the PTR and (* (void **) PTR have the same results? PTR is the same pointer. (void *) PTR and (* (void **) PTR are the same.
31: int main () {int x = 3; printf ("% d", x); return 1;}: Why does one return 1 Since the function is not called by other functions? Answer: in Mian, the C standard assumes that 0 indicates success, and a non-0 value indicates an error. The specific value is the specific error information in a certain project.
32. To assign a value to the absolute address 0x100000, we can use (unsigned int *) 0x100000 = 1234. What should we do if we want the program to jump to the absolute address 0 x for execution? Answer: * (void (*) () 0x100000) (); first, convert 0x100000 to a function pointer, that is, (void (*)()) 0x100000 and then call it: * (void (*) () 0x100000) (); typedef makes it more intuitive: typedef void (*) () voidfuncptr; * (voidfuncptr) 0x100000 )();
33. If an array table is known, use a macro definition to obtain the number of data elements. Answer: # define ntbl (sizeof (table)/sizeof (Table [0])
34. What are the differences between threads and processes? Does a thread have the same stack? Does the DLL have an independent stack? Answer: The process is dead, but only a collection of resources. Real program execution is completed by threads. When the program starts, the operating system will help you create a main thread. Each thread has its own stack. Whether there is an independent stack in the DLL. This question is hard to answer, or whether the question itself has a problem. Because the code in the DLL is executed by some threads, only the thread has a stack. If the code in the DLL is called by the thread in the EXE, does it mean that the DLL does not have its own stack? If the DLL code is executed by a thread created by the DLL itself, does the DLL have an independent stack? The above is about the stack. For the heap, if each dll has its own heap, it is best to delete it from the DLL if it is dynamically allocated from the DLL, if you allocate memory from the DLL and delete it in the EXE or another DLL, it is very likely that the program will crash.
35. Unsigned short a = 10; printf ("~ A = % u \ n ",~ A); char c = 128; printf ("c = % d \ n", c); how much is output? Analysis process answer: Question 1 ,~ A = 0xfffffff5; int value:-11, but uint is output. Therefore, output 4294967285 is the second question, c = 0x10, the output is int, the highest bit is 1, is negative, so its value is 0x00, the complement code is 128, so the output is-128. These two questions are all about the highest bit processing when binary is converted to int or uint.

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.