Embedded c language written test questions

In this article, let's take a look at the written test questions of embedded c language. The main content of this article is as follows:

Why does the embedded industry pay attention to the investigation of C language?

First of all, let's solve the first problem first, why does the embedded industry pay attention to the investigation of C language? Or why embedded development is developed in c language, and why is c language so popular in the embedded industry among so many programming languages?

C language's excellent portability and direct access to hardware make it very suitable for development and use in the embedded industry. In fact, there are assembly and C languages ​​that can directly access hardware, but assembly language is a low-level language, and it is difficult to complete some complex functions. This foreign exchange programming language is closely related to the architecture of the CPU. The assembly codes of the X86 architecture and the ARM architecture are different. It violates the principle of portability in embedded development. However, assembly is more efficient than C language in accessing hardware. Therefore, the work of hardware initialization is generally handed over to assembly, and more complex operations are handed over to C language.

The limitations of other object-oriented languages ​​and the inability to directly operate hardware make C language have an irreplaceable position and role in embedded development.

Several Topics of Common Embedded C Language Written Test Questions

type of data

1. Use variable a to define the following types of variables

(1) An integer

(2) A pointer to an integer

(3) A pointer to a pointer, the pointed pointer points to an integer

(4) An array with ten integers

(5) An array with 10 pointers, the array elements point to an integer

(6) A pointer to an array of 10 integers

(7) A pointer to a function that takes an integer parameter and returns an integer

(8) An array of 10 pointers to a function that takes an integer parameter and returns an integer

Answer:

(1)int a;

(2)int *a;

(3)int **a;

(4)int a[10];

(5)int *a[10];

(6)int (*a)[10];

(7)int (*a)(int );

(8)int (*a[10])(int );

Analysis:

There are two ways to test this question. One is to give the requirements and write the definition statement by yourself, and the other is to give the definition statement and write the meaning of the variable a by yourself.

The solution is to master a few principles. First, here is a picture for everyone. The picture below describes the priority of operators in C language.

Then look at what the essence of this variable is. The closer to the definition of the essence, the higher its priority. What does it mean? Let's analyze two variables to get a feel for

An array with 10 pointers, the array elements point to an integer: int *a[10];

A pointer to an array of 10 integers: int (*a)[10];

From the picture of operator priority above, we can know that the priority of [] is higher than *, so in int *a[10];, the essence of a is an array, and then the data members stored in the array are pointers, and the data members Points to integer data.

What about int (*a)[10]; Here, parentheses are used to enclose *a to emphasize that the essence of a is a pointer type, a points to an array, and the pointed array has ten integer data.

After distinguishing pointer arrays and array pointers, you can try to analyze other complex data types. You can look at the requirement to write a definition statement or look at the definition statement to write the meaning of variable a.

2. The following is a 32-bit C program under Linux/Windows, please calculate the value of sizeof

1.void func(char str[100]);  

2.int main(int argc, const char *argv[])  

3.{  

4.    char str[] = "hello";  

5.    char *p = str;  

6.    int n = 10;  

7.    char str_fun[100];  

8.    void*p_malloc = malloc(100);  

9.    printf("sizeof(str) = %d\n",sizeof(str));  

10.    printf("sizeof(p) = %d\n",sizeof(p));  

11.    printf("sizeof(n) = %d\n",sizeof(n));  

12.    func(str_fun);  

13.    printf("sizeof(p_malloc) = %d\n",sizeof(p_malloc));  

14.    return 0;  

15.}  

16.void func(char str[100])  

17.{  

18.    printf("sizeof(str) = %d\n",sizeof(str));  

19.}  

Answer:

1.sizeof(str) = 6  

2.sizeof(p) = 4  

3.sizeof(n) = 4  

4.sizeof(str) = 4  

5.sizeof(p_malloc) = 4  

Parse:

First of all, the first calculation is the byte length of the array str, str is the length determined while assigning the initial value, and its length is "hello\0" ​​with a total length of 6 characters, that is, 6 bytes.   

The second calculation is the byte length of the pointer p, no matter what data type the pointer points to, its byte length is 4. 

The third calculation is the byte length of the integer variable n. Under the 32-bit system, the integer data occupies 4 bytes. 

The fourth is the byte length of the formal parameter str calculated in the sub-function. For the formal parameter of the function, whether it is an array or a pointer, its essence is a pointer, so its byte length is 4 bytes.   

The fifth calculation is the byte length of the pointer p_malloc. Although it points to a memory space of 100 bytes, its essence is still a pointer, which is still 4 bytes.

Keywords and preprocessing

1. What is the role of the static keyword?

answer:

(1) In the function body, the scope of the static variable is the function. Unlike the auto variable, the memory of the variable is only allocated once, so its value will still maintain the last value when it is called next time.

(2) The static global variables in the module can be accessed by all functions in the module, but cannot be accessed by other functions outside the module.

(3) A static function in a module can only be called by other functions in this module, and the scope of use of this function is limited to the module in which it is declared.

2. What is the role of the const keyword

answer:

(1) To prevent a variable from being changed, you can use the const keyword. When defining the variable, it is usually initialized, because there is no chance to change it later.

(2) For pointers, you can specify the pointer itself, you can also specify the data pointed to by the pointer, or both can be specified as const.

(3) In the function declaration, const can modify the formal parameter, indicating that it is an input parameter, and its value cannot be changed inside the function.

3. What are the advantages of const over #define?

answer:

(1) The const-modified read-only variable has a specific data type, and the macro has no data type. The compiler can perform type safety checks on the former, and only perform character replacement on the latter, without type safety checks.

(2) The compiler usually does not allocate storage space for ordinary const read-only variables, but saves them in the symbol table, which makes it known during compilation that there is no operation of storing in read memory, making it more efficient higher

4. What is the difference between enumeration and #define macro?

answer:

Overview of Enums and Macros  

(1) Enumeration: refers to enumerating the values ​​of variables one by one, and the values ​​of variables are limited to the range of the enumerated values   

(2) #define The macro definition is to use a specified identifier to represent a string

The difference between enumeration and #define macro   

(1) Enumeration variables can be debugged in the compiler, but macro constants cannot be debugged   

(2) #define macro constants are simply replaced in the precompilation stage. Enumeration constants are determined at compile time   

(3) Enumerations can define a large number of related constants at a time, while #define macros can only define one at a time

5. What are the similarities and differences between typedef and #define macro?

answer:

(1) Similarities: Usually, it can be understood as an alias for a character, and a new character is used to replace the original character in the program   

(2) difference  

a. The actual meaning is different, #define is a string replacement, typedef is a different type for the type

b. There is no semicolon at the end of the sentence of the macro definition as the end mark, which is not performed during the compilation process itself, but has already been completed during the preprocessing process. It is difficult to find potential errors

Pointers and Segmentation Faults

1. What is the relationship between pointers, arrays and addresses?

answer:

(1) The array is stored in a continuous memory unit, and the array name is the first address of the continuous memory unit, and the address of the memory unit is a pointer, so the array name is also a pointer.

(2) The array is composed of multiple array elements, and the size of the continuous memory occupied by the elements is different according to the type of the array. The first address of an element of an array is the first address of the continuous memory unit it occupies.

(3) A pointer variable can point to either an array or an array element. Assign the array name or the address of the first element of the array to the pointer, and the pointer points to an array. If you want to make the pointer variable point to the i-th element, you can assign the first address of the i element to it.

2. How to use pointers to represent multidimensional arrays

answer:

For example: suppose a is the name of a two-dimensional array, and a represents the first address of the entire two-dimensional array

n a+i, a[i], (a+i), &a i are equivalent,  

a[i]+j=(*(a+i)+j) The value of this element is equal to *(*(a+i)+j)

3. How are secondary pointers applied to one-dimensional arrays?

answer:

int a=[10],*p1,**p2,i;

p1=a;

p2=&p1;

a[i]=*(*p2+i)=* (p1+i)=*(a+i)

String related functions

1.size_t strlen(const char *s); 

Function: Calculate the string length 

Parameters: s: character array 

Return value: returns the actual length of the string, excluding '\0'

1.//Implement it yourself

2.int my_strlen(const char *s)  

3.{  

4.    char * sc;  

5.    for (sc = s; *sc != '\0'; ++sc);  

6.    return sc - s;  

7.}  

2.char *strcpy(char *dest, const char *src); 

Function: copy the data of src to dest 

Parameters: dest: destination character array src: source character array 

Return value: the copied character array 

Description: The dest character array 1 must be large enough 

Before connection, both strings end with '\0'; after connection, the '\0' of dest is canceled, and '\0' is added at the end of the new string

1.//Implement it yourself  

2.char *my_strcpy(char *dest, const char *src)  

3.{  

4.    int i;  

5.    while(src[i] != '\0')  

6.    {  

7.        dest[i] = src[i];  

8.        i++;  

9.    }  

10.  

11.    dest[i] = '\0';  

12.    return dest;  

13.}  

3.char *strcat(char *dest, const char *src); 

Function: Append the data of src to dest 

Parameters: dest: destination character array src: source character array 

Return value: Same as the final dest 

Explanation: character array 1 must be large enough 

Before connection, both strings end with '\0'; after connection, '\0' in string 1 is canceled, and '\0' is added at the end of the new string

1.//Implement it yourself  

2.char *strcat(char *dest, const char *src)  

3.{  

4.    int i = 0, j = 0;  

5.    while(dest[i] != '\0'){  

6.        i++;  

7.    }  

8.    while(src[j] != '\0'){  

9.        dest[i++] = src[j++];  

10.    }  

11.    dest[i] = '\0';  

12.    return dest;  

13.}  

4.int strcmp(const char *s1, const char *s2); 

Function: compare the length of two strings 

Return value: If string 1< string 2, return a negative integer 

If string 1 > string 2, return a positive integer 

If string1 == string2, return zero 

illustrate: 

Compare the two strings one by one from left to right (ASCII code), until a different character or '\0' is encountered 

You cannot use "==" for string comparison, you must use strcmp

1.//Implement it yourself  

2.int my_strcmp (const char *s1, const char *s2)  

3.{  

4.  int ret;  

5.  while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++);  

6.  return ret;  

7.}  

linked list

The most complicated thing in the linked list is the two-way circular linked list. Show me the two-way circular linked list. I believe that if the two-way circular linked list can be understood thoroughly, the one-way linked list is no problem.

1. Two-way circular linked list

1.//linklist.h  

2.#ifndef __LINKLIST_H__  

3.#define __LINKLIST_H__  

4.#include   

5.#include   

6.//Doubly linked circular list  

7.typedef int datatype;  

8.typedef struct dnode{  

9.    datatype data;  

10.    struct dnode *prior;  

11.    struct dnode *next;  

12.}DLinkList, *DLinkList_p;  

13.DLinkList_p creat_dlinklist(void);  

14.DLinkList_p getnode_dlinklist(DLinkList_p D, int pos);  

15.int insert_dlinklist(DLinkList_p D, datatype value, int pos);  

16.void show_dlinklist(DLinkList_p D);  

17.int delete_dlinklist(DLinkList_p D, int pos);  

18.#endif  

1.//  linklist.c  

2.#include "linklist.h"  

3.DLinkList_p creat_dlinklist(void){  

4.    DLinkList_p D = NULL;  

5.    D = (DLinkList *)malloc(sizeof(DLinkList));  

6.    if(NULL == D){  

7.        printf("malloc error!\n");  

8.        return NULL;  

9.    }  

10.    D->data = 0;  

11.    D->prior = D;  

12.    D->next = D;  

13.    return D;  

14.}  

15.DLinkList_p getnode_dlinklist(DLinkList_p D, int pos){  

16.    if(NULL == D){ printf("D is NULL"); return NULL; }  

17.    if(pos < 0){ printf("pos error!\n"); return NULL; }  

18.    int i = 0;   

19.    DLinkList_p p = D->next;  

20.    while( p != D && i < pos){  

21.        p = p->next;  

22.        i++;  

23.    }  

24.    if(p == D){ printf("pos > length\n"); return NULL; }  

25.    if(i == pos){ return p; }  

26.    printf("pos > length!\n");  

27.    return NULL;  

28.}  

29.//Insert function  

30.int insert_dlinklist(DLinkList_p D, datatype value, int pos){  

31.    DLinkList_p new = NULL;    DLinkList_p Q = NULL;  

32.    if(NULL == D){ printf("D is NULL\n"); return -1; }  

33.    if(pos < 0){ printf("pos error!\n"); return -1; }  

34.    else if( D->next == D ){ Q = D; }  

35.    else{ Q = getnode_dlinklist(D, pos); }  

36.    if( NULL == Q ){ printf("Q get NULL\n"); return -1; }  

37.      

38.    new = (DLinkList *)malloc(sizeof(DLinkList));  

39.    if(NULL == new){  

40.        printf("malloc error!\n");  

41.        return -1;  

42.    }  

43.    new->data = value;  

44.    new->next = Q;  

45.    new->prior = Q->prior;  

46.    Q->prior->next = new;  

47.    Q->prior = new;  

48.    return 0;  

49.}  

50.//Delete  

51.int delete_dlinklist(DLinkList_p D, int pos){  

52.    DLinkList_p del = NULL;  

53.    if(NULL == D){ printf("D is NULL\n"); return -1; }  

54.    if(pos < 0){ printf("pos error!\n"); return -1; }   

55.    else{ del = getnode_dlinklist(D, pos); }  

56.    if( NULL == del ){ printf("Q get NULL\n"); return -1; }  

57.      

58.    del->prior->next = del->next;  

59.    del->next->prior = del->prior;  

60.    free(del);  

61. del = NULL;  

62.    return 0;  

63.}  

64.void show_dlinklist(DLinkList_p D){  

65.    if(NULL == D){ printf("D is NULL\n"); return; }  

66.    DLinkList_p p = D->next;  

67.    while(p != D){  

68.        printf("%d\n", p->data);  

69.        p = p->next;  

70.    }  

71.}  

Embedded Internet of Things needs to learn a lot. Don't learn the wrong route and content, which will cause your salary to go up!

Share a data package with everyone, about 150 G. The learning content, face-to-face scriptures, and projects in it are relatively new and complete! (Click to find a small assistant to receive)

Guess you like

Origin blog.csdn.net/m0_70911440/article/details/131591260