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 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.

2. Variable-length argument lists

Normally you declare a function to take a fixed number of arguments. But it is also possible to define functions capable of taking variable numbers of arguments. The standard C function printf() is a function of this sort. You can put almost any number of integers, floats, doubles, and strings in the format specifier part (after the string argument), and the printf() function will figure out what to do with them. Just like printf(), you can declare your own functions that contain a variable number of arguments.

3. Testing and setting individual bits

“Bit-twiddling”, or manipulating the individual bits of items such as integers, is sometimes considered to be a dark art used by advanced programmers. It’s true that setting individual bit values can seem a rather obscure procedure. But it can be useful, and it is a technique that is well worth knowing.

4. Short circuit operators

C’s logical operators, && (“and”) and || (“or”), let you chain together conditions when you want to take some action only when all of a set of conditions are true (&&) or when any one set of conditions is true (||). But C also provides the & and | operators. And it is vital that you understand the difference in how these work. In brief, the double-character operators (&& and ||) are called “short-circuit” operators. When used between two expressions, the second expression is only evaluated when the first expression is found to be true; otherwise it is skipped.

Many compilers are not this smart, however; so a test of this sort in a C program, where the evaluation of one part is dependent on the other to be true, can be a very bad idea!

5. Stacks – pushing and popping

A “stack” is a last-in, first-out storage system. You can use address arithmetic to add elements to a stack (pushing) or remove elements from the stack (popping). When programmers refer to “the stack”, they typically mean the structure that is used by the C compiler to store local variables declared inside a function.

6. Testing for header inclusion

C uses “header” (“.h”) files that may contain declarations of functions and constants. A header file may be included in a C code file by importing it using its name between angle brackets when it is one of the headers supplied with your compiler (#include < string.h >) or between double-quotes when it is a header that you have written: (#include “mystring.h”). But in a complex program containing many source code files, there is the danger that you may include the same header file more than once.

Tags: , , , , , , , , , , , , , , , , , , , , ,