Three Key Elements for Embedded Engineers to Master C Language

Three Key Elements for Embedded Engineers to Master C LanguageAs an embedded engineer, how can one write efficient and clear C language programs?

  • Utilize the C language mindset for program structure construction
  • Have a solid foundation in C language algorithms to implement the program’s logical structure
  • Flexibly use pointer operations in C language

Although the above statements may seem abstract and confusing, they essentially describe the process of using C language to encounter problems, analyze issues, and solve them.When embedded engineers write C language programs, they need to construct the program structure based on the problems encountered.For example, consider the classic problem of “Monkeys Selecting a King”: a group of monkeys stands in a circle, holding hands. Starting from any monkey, they count from 1, and when they reach a predetermined number to be excluded, that monkey exits the circle. The counting continues from the next monkey, and this process repeats until only one monkey remains, who becomes the king.Constructing Program Structure Using C Language MindsetThe program consists of three main parts:a. Data acquisition: To run the program, we need to obtain the total number of monkeys, the starting monkey, and the number to be excluded;b. Data computation: We need to exclude the corresponding data from a set of data, ensuring logical correctness;c. Improve the program’s execution speed by using pointers instead of loops.Logical Implementation Using C Languagea. Data acquisition: Use printf and scanf to obtain parameters.

/* Read input conditions */printf("input total num:");scanf("%d", &n);printf("from which num begin:");scanf("%d", &k);if(k>n||k==0){printf("please input the right begin num");return 1;}printf("input the out num:");scanf("%d", &m);if(m>n||m==0){printf("please input the right del num");return 2;}

Also, pay attention to exception handling. For example, the two if statements above check for exceptions, with each exception corresponding to a different return value, facilitating debugging and ensuring data validity.b. /* Define linked list node type */

typedef struct node {int data; struct node *next;} linklist;

Construct a circular linked list to represent the “monkey circle”.

/* Create circular linked list, head node also stores information */head = (linklist*) malloc(sizeof(linklist));p = head;p->data = 1;p->next = p;/* Initialize circular linked list */for (i = 2; i <= n; i++){s = (linklist*) malloc(sizeof(linklist));s->data = i;s->next = p->next;p->next = s;p = p->next;}

After this step, both head and p (present) become a linked list of “monkeys”. During the construction of this linked list, it is important to note a few points: memory allocation should follow the principle of allocating as much as needed. Allocating too much at once can lead to memory leaks, but this small program is unlikely to encounter such issues. Additionally, familiarize yourself with the construction method of circular linked lists: the tail of the list points to the head. At this point, one might also think of the case of doubly linked lists.c. /* Find the k-th node */

p = head;for (i = 1; i <= k; i++){p = p->next;}

Find the starting position for counting. At this point, p points to the starting “monkey”. Since a linked list is used, this process only requires focusing on the next pointer of p.a. Save the initial parameters of the “monkey” circle.

/* Save total number of nodes */total = n;printf("\nthe out num:");q = head;

Why is this necessary? First, to control the number of monkeys, we retain the total count. Secondly, q (query) is used to retain the linked list before the excluded monkey and connect it to the linked list after the excluded monkey. This way, the deletion of elements in the circular linked list is completed.b. Monkeys counting.Counting is the key task of the entire program, which needs to accomplish the following: a. Find the starting “monkey”; b. Delete that “monkey”; c. Connect the head and tail of the deleted circular linked list.

/* Stop loop when only one node remains */while (total != 1){/* Counting process, p points to the node to be deleted */for (i = 1; i < m; i++){p = p->next;}/* Print the number of the node to be deleted */printf("[%d] ", p->data);/* q points to the predecessor of p node */while (q->next != p){q = q->next;}/* Delete p node */q->next = p->next;/* Save pointer to deleted node */s = p;/* p points to the successor of the deleted node */p = p->next;/* Free the deleted node */free(s);/* Decrease node count by one */total--;}/* Print the last remaining node's number */printf("\n\nthe last num:[%d] \n\n", p->data);free(p);}

Through the above data computation, the corresponding elements of the linked list can be deleted, which perhaps showcases the charm of C language programming.Using Pointers in C LanguageFor example, the definitions of linked list pointers in this program: p, s, q.

linklist *head, *p, *s, *q;

We know that pointer operations can not only reduce the memory required for data operations but also improve the execution speed of the program.The advantages of pointers may not be obvious in this program, but they become significant in cases with large data sets and high-speed operation requirements, such as in the Linux kernel.In summary, for embedded engineers, being able to design good hardware circuits and also write good software programs will make our lives even better.

Leave a Comment