C Pro­gram­ming Tips for New­bies

Many pro­gram­mers be­gin their ro­mance with code by learn­ing C. The au­thor sug­gests a few tips for new­bies so they can avoid bad cod­ing prac­tices.

OpenSource For You - - Contents -

The C pro­gram­ming lan­guage is usu­ally the first one that stu­dents are taught in most col­leges. It is one of the most widely used lan­guages, par­tic­u­larly in sys­tems pro­gram­ming. Here are a few sim­ple tips to avoid some of the most com­mon pit­falls in the lan­guage.

The scanf func­tion and char­ac­ter in­put

One com­mon er­ror is due to the char­ac­ters re­main­ing in the key­board buf­fer af­ter an in­put is ex­e­cuted by a scanf state­ment.

When there are two or more con­sec­u­tive scanf state­ments in the pro­gram and the sec­ond or later scanf state­ment is for char­ac­ter in­put, it looks as if the scanf state­ment is skipped. This is be­cause the new­line char­ac­ter, \n, which was in­put due to the re­turn key be­ing pressed by the user, re­mains in the key­board buf­fer. If the next scanf is for char­ac­ter in­put, the new­line char­ac­ter is taken as the in­put for the char­ac­ter vari­able.

scanf(“%d”, &num); scanf (“%c”, &chr);

This does not hap­pen for the in­te­ger in­put, as the %d sym­bol for in­te­gers forces all lead­ing white spa­ces and spe­cial char­ac­ters in the in­put stream to be skipped.

This sit­u­a­tion can be avoided by us­ing a space be­fore %c. The space char­ac­ter will en­sure that the scanf state­ment skips the lead­ing white spa­ces and spe­cial char­ac­ters, and the above prob­lem can be avoided.

scanf(“%d”, &num); scanf (“%c”, &chr); /*space char­ac­ter added be­fore %c */

The fflush func­tion

The fflush func­tion can also be used for flush­ing the key­board buf­fer.

fflush (stdin);

But the fflush func­tion is avoided by many as it is un­de­fined in the C stan­dard for the in­put stream.

Magic num­bers

When a num­ber sud­denly ap­pears in some part of a pro­gram with­out any back­ground or ex­pla­na­tion, it is said to be a magic num­ber. The in­tro­duc­tion of magic num­bers is a re­sult of poor pro­gram­ming prac­tices and also due to writ­ing a pro­gram with­out think­ing it through.

For ex­am­ple:

for (i=0; i<10; i++) scanf(“%d”, &a[i]);

The above code is used to in­put an ar­ray of in­te­gers of size 10. The num­ber 10 has to be used re­peat­edly to men­tion

the size of the ar­ray when pro­cess­ing it. Also, the re­lated num­bers have to be used, like 10-1, 10+1, etc.

If the size of the ar­ray has to be changed for some rea­son, chang­ing the num­ber ‘10’ to some other num­ber has to be done in mul­ti­ple places. This in­tro­duces a scope for er­ror, as there is a chance that one or more oc­cur­rences of the num­ber may be missed.

Defin­ing the list size us­ing #de­fine will avoid this prob­lem, as shown below:

#de­fine SIZE 10

The magic num­ber is avoided in the code. Any change in the num­ber can also be im­ple­mented with a change in a sin­gle place, which takes ef­fect through­out the code.

Con­stant on the left hand side

When check­ing for equal­ity, one of the “=” signs is in­ad­ver­tently left out and the as­sign­ment op­er­a­tor “=” is typed in­stead of “==”, the lat­ter be­ing the re­la­tional op­er­a­tor to test equal­ity in a com­par­i­son state­ment. But it is not an er­ror as per the C com­piler and is treated as an as­sign­ment state­ment. Hence, the if state­ment re­turns true as long as the value as­signed is not zero, and this bug is dif­fi­cult to de­tect.

The above lines as­sign 10 to the vari­able ‘i' and then test whether the value of i is true (non-zero) or false (0). Since the value of i al­ways re­sults in 10, the re­sult of the Boolean ex­pres­sion will al­ways be true.

The best way to pre­vent such bugs in the pro­gram is to write the con­stant on the left hand side. The com­piler will flag an er­ror if “=” is writ­ten in­stead of “==” since a con­stant can­not be as­signed a value.

Nam­ing of vari­ables

Care should be taken when nam­ing vari­ables. It is bet­ter to use i, j, k, m and n as names of in­te­ger vari­ables; x, y and z for float (or dou­ble) vari­ables; and a, b and c as names of ar­rays or co­ef­fi­cients so as to make the pro­gram easy to read and to re­duce the ef­fort to un­der­stand the code.

Avoid us­ing sim­i­lar look­ing char­ac­ters like 1 and l as these could be in­ter­preted wrongly by the reader1.

Be­gin­ners can go through the best prac­tices for the nam­ing of vari­ables and func­tions, and fol­low a par­tic­u­lar stan­dard, con­sis­tently.

Avoid us­ing the gets func­tion

Many be­gin­ners use the gets func­tion to in­put a string. Since it does not have a way to limit the size of the in­put string, in­put data can be big­ger than the al­lo­cated space for the in­put, which will re­sult in buf­fer over­flow. The gets func­tion has been dep­re­cated in the C99 stan­dard, and has been re­moved from the lat­est C11 stan­dard. I am writ­ing about the gets func­tion as I have seen Turbo C be­ing used even by MCA stu­dents in a few col­leges in In­dia!

The fgets func­tion can be used to avoid the above men­tioned prob­lem, as the max­i­mum length of the char­ac­ters that can be read is given as an ar­gu­ment to the func­tion. In the above code, stdin is the in­put stream.

Com­ment­ing out blocks of code

The C lan­guage does not al­low nest­ing of com­ments. Hence, the /* and */ sym­bols can­not be used to com­ment out sec­tions of the code tem­po­rar­ily for test­ing pur­poses.

For­tu­nately, the #if... # en­dif pre­proces­sor direc­tive can be used to leave out blocks of code from be­ing com­piled.

The above method makes it easy to com­ment out large sec­tions of code tem­po­rar­ily.

Ref­er­ence

[1] In­tro­duc­tion to com­puter science- An al­go­rith­mic ap­proach by Tremblay and Bunt. McGraw- Hill in­ter­na­tional edi­tion

By: S. Sathya­narayanan

The au­thor works as in­for­ma­tion sci­en­tist in the Sri Sathya Sai In­sti­tute of Higher Learn­ing, Brin­da­van Cam­pus, Ben­galuru, and also heads the com­puter cen­tre of the cam­pus. He has more than 25 years of ex­pe­ri­ence in sys­tems ad­min­is­tra­tion and in teach­ing IT cour­ses. He is an en­thu­si­as­tic pro­moter of FOSS and can be reached at sathya­[email protected]­hoo.com/ and ssathya­[email protected]­sihl.edu.in.

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.