Simple C Programming Tips for Newbies

2
12562

Many programmers begin their romance with code by learning C. The author suggests a few tips for newbies so they can avoid bad coding practices.

The C programming language is usually the first one that students are taught in most colleges. It is one of the most widely used languages, particularly in systems programming. Here are a few simple tips to avoid some of the most common pitfalls in the language.

The scanf function and character input

One common error is due to the characters remaining in the keyboard buffer after an input is executed by a scanf statement.

When there are two or more consecutive scanf statements in the program and the second or later scanf statement is for character input, it looks as if the scanf statement is skipped. This is because the newline character, \n, which was input due to the return key being pressed by the user, remains in the keyboard buffer. If the next scanf is for character input, the newline character is taken as the input for the character variable.

scanf(“%d”, &num);

scanf (“%c”, &chr);

This does not happen for the integer input, as the %d symbol for integers forces all leading white spaces and special characters in the input stream to be skipped.

This situation can be avoided by using a space before %c. The space character will ensure that the scanf statement skips the leading white spaces and special characters, and the above problem can be avoided.

scanf(“%d”, &num);

scanf (“ %c”, &chr); /*space character added before %c */

The fflush function

The fflush function can also be used for flushing the keyboard buffer.

fflush (stdin);

But the fflush function is avoided by many as it is undefined in the C standard for the input stream.

Magic numbers

When a number suddenly appears in some part of a program without any background or explanation, it is said to be a magic number. The introduction of magic numbers is a result of poor programming practices and also due to writing a program without thinking it through.

For example:

for (i=0; i<10; i++)

scanf(“ %d”, &a[i]);

The above code is used to input an array of integers of size 10. The number 10 has to be used repeatedly to mention the size of the array when processing it. Also, the related numbers have to be used, like 10-1, 10+1, etc.

If the size of the array has to be changed for some reason, changing the number ‘10’ to some other number has to be done in multiple places. This introduces a scope for error, as there is a chance that one or more occurrences of the number may be missed.

Defining the list size using #define will avoid this problem, as shown below:

#define SIZE 10

The magic number is avoided in the code. Any change in the number can also be implemented with a change in a single place, which takes effect throughout the code.

Constant on the left hand side

When checking for equality, one of the “=” signs is inadvertently left out and the assignment operator “=” is typed instead of “==”, the latter being the relational operator to test equality in a comparison statement. But it is not an error as per the C compiler and is treated as an assignment statement. Hence, the if statement returns true as long as the value assigned is not zero, and this bug is difficult to detect.

If (i = 10) /* beg your pardon for using a magic number! */

{

….

….

}

The above lines assign 10 to the variable ‘i’ and then test whether the value of i is true (non-zero) or false (0). Since the value of i always results in 10, the result of the Boolean expression will always be true.

The best way to prevent such bugs in the program is to write the constant on the left hand side. The compiler will flag an error if “=” is written instead of “==” since a constant cannot be assigned a value.

If (10 == i)

{

….

….

}

Naming of variables

Care should be taken when naming variables. It is better to use i, j, k, m and n as names of integer variables; x, y and z for float (or double) variables; and a, b and c as names of arrays or coefficients so as to make the program easy to read and to reduce the effort to understand the code.

Avoid using similar looking characters like 1 and l as these could be interpreted wrongly by the reader1.

Beginners can go through the best practices for the naming of variables and functions, and follow a particular standard, consistently.

Avoid using the gets function

Many beginners use the gets function to input a string. Since it does not have a way to limit the size of the input string, input data can be bigger than the allocated space for the input, which will result in buffer overflow. The gets function has been deprecated in the C99 standard, and has been removed from the latest C11 standard. I am writing about the gets function as I have seen Turbo C being used even by MCA students in a few colleges in India!

The fgets function can be used to avoid the above mentioned problem, as the maximum length of the characters that can be read is given as an argument to the function.

#define MAX 20

….

char name[MAX];

…

…

fgets (name, MAX, stdin);

…

In the above code, stdin is the input stream.

Commenting out blocks of code

The C language does not allow nesting of comments. Hence, the /* and */ symbols cannot be used to comment out sections of the code temporarily for testing purposes.

Fortunately, the #if… # endif preprocessor directive can be used to leave out blocks of code from being compiled.

#if 0

……..

……..

#endif

The above method makes it easy to comment out large sections of code temporarily.

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here