C Programming Tips and Tricks for Beginners – Top 15

We’ve chalked some of the best C Programming Tips and Tricks in this article.

If you are a “C” learning student or a “C” programmer, then these tips are for you and can come really handy in your work assignments.

We discovered some of these ideas after lots of reading and some while working. And all of our tips are based on real time use cases that will help you solve real-time issues.

If you also have a few ideas in store, then don’t mind sharing them with us. Use the comment box section and let others know as well.

Also, you can participate in our knowledge driver by distributing this post to your social circle and friend groups.

After this post, you can even try evaluating your C/C++ programming skill by going through the below online quiz.

Let’s now start looking into the tips one by one.

C Programming Tips and Tricks Every Programmer Should Know.

Top C Programming Tips and Tricks.

Top C Programming Tips and Tricks.

C Programming Tips (1) – Macro to get array size of any data type.

The following macro will help you in getting the size of an array of any data type.  It works by dividing the length of the array to the size of its field.

#define NUM_OF(x) (sizeof (x) / sizeof (*x))


C Programming Tips (2) – Calculate elapsed time.

Friends, have you ever needed to calculate the time passed between two events? or You wanna keep a check on some function which is spuriously taking extra execution time than expected?

Here is the code snippet implemented using a set of macros to help you figure out how long something will take to run.


C Programming Tips (3) – Smart Random Number Generator.

We have this rand() function defined in the stdlib.h for random no. generation. Did you use it and realized that every time you run your program but it returns the same result.

It’s because, by default, the standard (pseudo) random number generator is seeded with the number 1. To have it start anywhere else in the series, call the function srand(unsigned int seed).

For the seed, you can use the current time in seconds.

For your note, the above code seeds the generator from the current second of time. This implies that if you expect your program to re-run more than once a second, the given code may not fulfill your requirement. A possible workaround is to store the seed in a file (that you will read later from your program), and you then increment it every time the program is run.


C Programming Tips (4) – Heard of “goes to-->” Operator?

Actually --> is not an operator. In fact, it’s a combination of two separate operators i.e. -- and >. To understand how “goes to” operator works, go through the below code snippet.

In the example, there is conditional’s code which decrements variable x, while returning x’s original (not decremented) value, and then compares the original value with 0 using the > operator.


C Programming Tips (5) – Some Cool SCANF Tricks.

Find out some of the unheard scanf tricks that you must know.


C Programming Tips (6) – Call functions at program termination

Did you know about the atexit() API? This C API is used to register functions that can be automatically called when the program finishes its execution.

For example –

Notice that foo and bar functions haven’t been called but are registered to be called when the program exits. Such functions should not return anything nor accept any arguments. You can register upto 32 such functions. They’ll be called in the LIFO order.


C Programming Tips (7) – Initialize a 2-D array with a long list of values.

It can be easily achieved by keeping the list values into a file and then store the file content into 2-D array with the following line of code.


C Programming Tips (8) – Add any numbers without “+” operator.

Bitwise operators can be used to perform the addition (+) operation as mentioned in below example:


C Programming Tips (9) – Swapping two variables without any temp variable.

There are three ways to do this which I’ve mentioned below.

To swap two variables without using additional space or arithmetic operators, you can simply use the xor operator.


C Programming Tips (10) – Put the constant as the first term while doing comparisons.

Sometimes, we tend to confuse “=” operator with “==” operator. To avoid this, use the defensive programming approach. 0==x instead of x==0 so that 0=x can be caught by

It means you should write “1==x” instead of “x==1” so that the compiler will always flag an error for the miswritten “1=x”.

So whenever you will mistakenly write the following.

The compiler will complain and refuse to compile the program.

While it’s not possible if you are comparing two variables. For example, the expression

can be miss written as


C Programming Tips (11) – Quick Commenting.

Sometimes you may find yourself trying to comment blocks of code which have comments within them. Because C does not allow nested comments, you may find that the */ comment end is prematurely terminating your comment block.

You can utilize the C Preprocessor’s #if directive to circumvent this:


C Programming Tips (12) – Use of Conditional Operator.

We mostly use it in the following form:

But it could also be used as:


C Programming Tips (13) – Arrays and Pointers not entirely the same.

Many of us tend to misunderstand a concept that pointers and arrays are the same. They are not. Pointers are merely variables holding the address of some location whereas an array is conceptualized as a sequence of memory locations of a type.

At compile time, an array is an array. Only during run-time, an array devolves to a pointer.

To prove this fact, let me show you an example.


C Programming Tips (14) – Pointer to array and Array of Pointers.

Let’s check out an interesting comparison between the following three declarations.

Here in int *ptr1[5], ptr1 is an array of 5 integer pointers (An array of int pointers).

And in int (*ptr2)[5], ptr2 is a pointer to an array of 5 integers (A pointer to an array of integers).

It’s same as ptr1 (An array of int pointers).


C Programming Tips (15) – Log off computer Using C.



While starting to write this article, we thought to present you with 10 best of the C programming tips but ended up in delivering 15 wonderful tips.

We tried to cover those tips which you can relate to and are usable in your production environment. For your information, we’re deeply inspired by the father of C “Dennis Ritchie“. You can also follow him @ his wiki page.

Before we conclude, a humble request to share this post with your friends. And also leave your valuable feedback in the comment box at the end of this post.

You are most welcome to ask questions about this post which we’ll be more than happy to answer.



Leave a Reply