Common C programming errors


Here are some common programming errors made in C. Please add on…
1) Not initializing pointers
Anytime you use a pointer, you should be able to answer the question: What variable does this point to? If you cannot answer this question, it is likely it doesn’t point to any variable.
This type of error will often result in a Segmentation fault/coredump error on UNIX/Linux or a general protection fault under Windows.
char * st;   /* defines a pointer to a char or char array */
strcpy(st, “abc”);  /* what char array does st point to?? */
2) String Errors
Confusing character and string constants
char ch = ‘A’;     /* correct */
char ch = “A”;     /* error   */
3) Comparing strings with ==
Never use the == operator to compare the value of strings! Strings are char arrays. The name of a char array acts like a pointer to the string
4) Not null terminating strings
C assumes that a string is a character array with a terminating null character. This null character has ASCII value 0 and can be represented as just 0 or ‘\0’. This value is used to mark the end of meaningful data in the string. If this value is missing, many C string functions will keep processing data past the end of the meaningful data and often past the end of the character array itself until it happens to find a zero byte in memory.
5) Not leaving room for the null terminator
A C string must have a null terminator at the end of the meaningful data in the string. A common mistake is to not allocate room for this extra character. For example, the string defined below
char str[30];
only has room for only 29 (not 30) actually data characters, since a null must appear after the last data character.

Refer the following link for more:

www.andromeda.com/people/ddyer/topten.html

Have any Question or Comment?

Leave a Reply

Exciting Offers

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 90 other subscribers

Exciting Offers

Exciting Offers

Exciting Offers

Close
%d bloggers like this: