10 Best Tips and Tricks C Programming for Every Programmer

Posted on

C is one of the most important and widely used of all programming languages. It is a powerful language that can be used not only to build general-purpose applications but also to write “low-level” programs that interact very closely with the computer hardware. C lets the programmer do things that many other languages do not. This means that good C programmers are able to write clever, efficient code.

However, there is a downside: while many other languages, such as C# or Java, may try to protect you from writing dangerous code that could crash your programs, C often lets you write just about any code you want-even allowing you to code in mistakes that will end in disaster.

In some ways, developing in C is the programming equivalent of a high-wire act-without a safety net. Experienced C programmers have all kinds of tricks to make the most of the C language. Here is a list of the top 10 tips and tricks for both new and experienced C programmers.

1. Function pointers

Sometimes it is useful to store a function in a variable. This isn’t a technique that is normally used in day-to-day programming, but it can be used to increase the modularity of a program by, for example, storing the function to be used in handling an event in the event’s data (or control) structure.

The key here is to define a type, “pointer-to function-returning-something” and then use that as a variable declaration-it makes the code a lot easier to read. Let’s consider a simple example. First I define a type PFC, which is a Pointer to a Function returning a Character:

typedef char (*PFC)();

This is then used to create a variable z:

PFC z;

I define a function a():

char a() {
      return 'a';
}

The address of this function is then stored in z:

z = a;
PrevNext

Leave a Reply

Your email address will not be published. Required fields are marked *