Monday , January 23 2017
Home / Embedded Systems / Cool C Programming Tricks For Embedded Software Developers

Cool C Programming Tricks For Embedded Software Developers

Proficiency in C is an essential skill set for embedded software developers. In this post we will see some really cool C programming tricks that you should have already known. If you didn’t, it’s time to take a pen and paper.

Be warned this is just a small collection of routines that will help embedded software developers write better code. Of course I have listed only those that came to my mind while writing this article and I most probably left out something much better. In case something pops up in your head, leave a comment and I will update this post.

After finishing an intensive, week long, hands-on session on C programming, my teacher said “I have taught you all that is there to know about the syntax in C. This is one of those things that will take days to learn and  a lifetime to master. Now it up to you to practice and perfect”.

Practice and Perfect‘ is the unsaid mantra of C. The more you write, the better you get at it. I’m no professional at C. Hence I will not be writing any posts on C programming as such. There are a lot of resource on the internet and books on shelves for learning C.

For learning C from it’s very basic, I would suggest C and UNIX: Tools for Software Design by Martin L. Barrett, Clifford H. Wagner The book covers a lot about C and using GCC to compile C programs in a Linux machine. Linux is mostly written in C and hence provides a native environment for the language. If you are really interested, you can find some books and links to follow for getting started with Linux here.

Rotate left or right in C

In assembly there are direct instruction for rotate left and rotate right. But in C there is no straight forward method to do so. Here is a snippet that will let you do just that. And the best part is that, PIC C compiler will automatically know what you are trying to do and will replace this line of C with the corresponding assembly instruction.

The val << 7  is used as I assumed a 8 bit variable is being rotated. If you were to rotate a 16 bit value , use val << 15  instead.

Bit Set and Bit Clear in Macro

In embedded software applications, we have a constant need for setting and clearing bits. Some of us feel like using the mask and set/clear strategy each time there is a need to set and clear bits. But when working with 32 bit systems (ARM), it is not practical to write masks each time we need to set/clear a single bit. That’s when functions are written to handle this. But having function will occupy memory. A better alternative for this is to have code macros to take care of this at compile time.

You can use this function in you program like this, BitSet( myVar, 5); this will set bit 5 of myVar. Similarly BitClr(myVar,5); will clear bit 5 of myVar.

Bitfields

In embedded systems, most of the time there is a need to access the individual bits of a byte (in 8 bit system) or group of bytes (16 or 32 bit system). One way of doing this is by using the shift operations. The other ways is to use the bit fields in C.

The bit fields is a lesser known feature of the C programming language (at least to a common user). It comes in handy for low level firmware developers (system software) to have bit level access to the registers. If you take a look at the device specific header file for your favorite microcontroller you will find that most of the code is structure and union definitions much like what is below.

I will take the liberty to assume you have a working knowledge on structures and unions to understand bit fields. The size of a structure is the sum of the size of all its member variables where as in the case of a union, its size is given by the size of its biggest member. Union is used when only either of its member is needed at any time.

Here, you can set the value of data to something and then access each bit in the byte by using the variables of the anonymous union bit0 to bit7.

Duality of Array names

You should have heard about the duality of light, but duality of array names??

Yes, Array names are not just the label for the group of elements of similar data types. It is also a pointer to the base element of the array. All normal pointer reference and arithmetic to array label is allowed. But this pointer is a constant pointer and cannot be modified by the user.

ie., if you have an array test_arr[5] = { 1, 2, 3, 4, 5 }; then test_arr is a pointer that has the address of the first element of the array. So a difference like, *test_arr will return 1 and *(test_arr+2) will return 3.

Interoperability of array index

In C, if you had an array  int arr[5]; you could access it with an index ‘i’ and assign values to it. That is you can write  arr[i] = 7;

Turns out you could swap the array names and array index. That means you could write the same statement as  i[arr] = j;  and its perfectly valid C code.

Passing an array to functions and returning them

Arrays are one of the best way to store elements of similar datatypes. The elements are stored in sequential memory blocks hence making pointer operations possible. In fact it would have been useless to have pointer arithmetic without having arrays.

Single variables can be passed directly to a function as a parameter. But in case of an array, the pointer to the first element is sent to the receiving function. This pointer is used to navigate the array.

When a flow of control reaches the end of a function, the variables created in that function are destroyed and no longer accessible. So returning an array from a function is not possible. There are two ways for tackling this issue.

One way is by using a globally declared array and let the functions make use of this array. But using globally declared arrays is not always possible and is not a good practice. There comes the second and better alternative, while calling a function give it the pointer of the array defined in the calling function. This way, the array is not declared globally and still is available for the calling function locally.

Not clear? consider this case, we have 2 arrays which has to be added in a separate function and store them in another array. Here is how you can do this,

What did I miss?

Have I missed anything that you feel is a really neat trick? In what way does it improve the coding efficiency of an embedded engineer? Let’s start the discussion in the comment section.

Update #1: Paolopat said,

Sometimes I have to swap two variables…
You can avoid using a temporary variable using “XOR swap” like following :

Wow, that is a really cool trick! You can now swap variable without using the conventional  temp = a; a = b; b = temp;. As vijay pointed out, you could also do it in one line as, a ^= b ^= a ^= b; Which looks sleek but is rather cryptic.

About Siddharth

Siddharth is a Firmware Engineer, techie, and a movie-buff. His interests include, Programming, Embedded Systems, Linux, Robotics, CV, Carpentry and a lot more. At times, you could see some of his sunday projects converge on release quality. You get to know him on the following social channels.

Check Also

c2000_lauchpad

Interfacing 16×2 LCD with C2000 launchpad

  The C2000 Launchpad is an inexpensive evaluation module based on Piccolo family microcontrollers from …

  • Sometimes I have to swap two variables…
    You can avoid using a temporary variable using “XOR swap” like following :

    a = a ^ b
    b = a ^ b
    a = a ^ b

    • Paolopat, Thanks for sharing a really cool method for swapping variables. It’s indeed a neat trick that should have been on that list. I Have updated the post with your submission.

    • vijayakumar

      Nice one Paolopat …
      same thing in a single line,
      a^=b^=a^=b

  • nice coool one .but here i gotta doubt , “|=” “&=” what is the meaning of these two operators, which you mentioned in “bit clear and bit set in macro” topic .

    • Thanks Uma, a |= 10; is like writing a = a | 10; its a way of reducing the effort taken while coding. I think you can correlate the other similar operators.

  • Nice and great blog siddharth . Keep it up.

  • rajesh

    awesom…!!!! i realy got to know many things to b frank….

Keep in touch with the current trends!
Did you like this article? Sign up and get our latest posts delivered to your inbox!
  We hate spam and never share your details.