CategoryProgramming

Inorder, Preorder and Postorder Traversal using C Programming

A major operation, which we often apply in a binary tree is the traverse. There are three different methods for traversing a tree: Preorder Inorder Postorder The following code snippets are these methods in C : void preorder_traversal(struct node_t * node) { if (node == NULL) return; printf("%dn", node->value); preorder_traversal(node->left); preorder_traversal(node->right); } void...

Simple and effective Semaphore for C++

A semaphore object is a synchronization object that maintains a count between zero and a specified maximum value. The count is decremented each time a thread completes a wait for the semaphore object and incremented each time a thread releases the semaphore. When the count reaches zero, no more threads can successfully wait for the semaphore object state to become signaled. The state of a...

CRC8 Simple Algorithm for C

The following code snippet is about CRC8 hash function. Usually in embedded systems there is an already built-in function for CRC8-16-32 etc. However in case there is not, here is a simple approach for it. CRCs are so called because the check (data verification) value is a redundancy (it expands the message without adding information) and the algorithm is based on cyclic codes. CRCs are popular...

Using Shared Memory in Linux Systems

Shared Memory is an efficient means of passing data between programs. One program will create a memory portion which other processes (if permitted) can access. A process creates a shared memory segment using shmget()|. The original owner of a shared memory segment can assign ownership to another user with shmctl(). It can also revoke this assignment. Other processes with proper permission can...

How to convert a number to C string

In certain embedded systems, using the strtol function to convert an integer to a C string may pose challenges. However, with the utilization of the provided C function, one can successfully convert any string to an integer number. While it may not be considered the most optimal solution, it proves beneficial in addressing such issues. This alternative approach can serve as a practical workaround...

How to left-trim a C string

In various instances, there’s a need to eliminate leading white spaces from a C-string. The provided code snippet efficiently accomplishes this task using a straightforward approach. By employing this uncomplicated method, unnecessary white spaces at the beginning of the C-string are effectively stripped away. This simplistic solution proves useful in scenarios where a basic yet effective...

C-strings merging

This handy code snippet plays a crucial role in merging C strings seamlessly. By leveraging the built-in C-string function strcpy, it allows us to effortlessly combine various strings into a single one, all without the hassle of manually managing memory space. The simplicity of this approach ensures efficiency in string concatenation. I t’s worth noting that the implementation of this...

How to convert a C-string to upper/lowercase

Often, I find myself in situations where I need to convert a string to either uppercase or lowercase. A nifty trick I often employ is leveraging the ASCII code, where each character corresponds to a specific number. The beauty lies in the fact that the uppercase letters (A-Z) and lowercase letters (a-z) are neatly organized into continuous ranges of ASCII values. For instance, a quick visit to...

How to reverse a C string

In this example we delve into a straightforward function designed to reverse a C-string. As we traverse the theoretical realm, it’s crucial to understand that a C string is essentially a character array culminating with the distinctive termination character ‘\0’ at the end of the actual string. To grasp the intricacies, consider that the length of the array housing the actual...

Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags