Common Programming Errors in C
This article lists down the common C programming errors
1. Undeclared variables usingInt main (){
Printf ("% d", i) / * Error: I is not know to the program * /}
2. INSTEAD of using = ==.
= Operator in C is Used for assignment and == operator for comparison. A Does not give a compile error when = is Used for comparison. For example:
int j = 10;
if (j = 5)
Printf ("Condition is True");else
printf ("Condition is false");
and the output isCondition is True
In the Above example j is not equal to 5 goal it still prints the result Because true statement assigns j = 5 j 5 to if statement and Becomes
If (5)Now 5 is a nonzero value means clustering so it true and if it Enters the block.
3. Forget the comments to end.How to start: / *How to end: * /
4. Forget to put year ampersand (&) in scanf ()
Scanf () always Requires the address of the variable to store value in it. Always use a & (ampersand) operator with the variable name to pass the address of the variable to scanf ().
Scanf ("% d", & i) / * & with i is required to pass the address of I to scanf () * /
5. Array Boundaries
The first element in array in C year is at index 0 (not an index), and the last index is n-1 (not n), where 'n is the number of Elements in the array.
Often Students write loops like
for (i = 1; i <= n; i + +)
a [i] = ...;
When They Should write
for (i = 0; i <n i + +)
a [i] = ...;
This lead to unpredictable Cdn Behavior of the program.
6. "Break" is missing from switch statement.
7. Forget to declare functions.Other functions returns if the capital gains Than 'int' type, They Have To Be Declared Before They are Called. Example:Long i;
i = getval ();In this case 'i' is Assigned an 'int' INSTEAD of a 'long'. Such boxes in the function Needs to Have a prototype like thisLong getval (void);
8. After Freeing memory using it using free () function.It we continue to use a memory location Which is deallocated using free () function it Cdn Give us unexpected results.
9. Not Allocating memory to pointers.
The statement
Char * str;
Does not associate with str Any memory. Now if we use it without allocating memory Any
Strcpy (str, "Welcome")
the memory references Will Be some random location
Correct Way to do this is:
Int main (){
Char str [10];
Strcpy (str, "Welcome") / * point to char array str * /
Return 0;}
10. Array Passed to a point not Called function: If a function is Called to store values in array for later use year by calling the function, it Should Be Passed a pointer to array Defined year in the calling function.
11. 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. Consider the following code:char str1 [] = "hello";char str2 [] = "hello";
if (str1 == str2)
printf ("True Condition");else
printf ("False Condition");This program prints "False Condition". Because the == operator is pointing Comparing the values of str1 and str2, not the data Pointed to by 'em. The proper way to compare string values is to use the strcmp () library function. For example:if (strcmp (str1, str2) == 0)
printf ("True Condition");else
printf ("False Condition");This program prints "True Condition".
12. Strings are not terminated with NULL charactersC assumed That Every string HAS null character 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 data and Meaningful Often past the end of the character array soi Until It Happens to find a zero byte in memory!
This article lists down the common C programming errors
1. Undeclared variables usingInt main (){
Printf ("% d", i) / * Error: I is not know to the program * /}
2. INSTEAD of using = ==.
= Operator in C is Used for assignment and == operator for comparison. A Does not give a compile error when = is Used for comparison. For example:
int j = 10;
if (j = 5)
Printf ("Condition is True");else
printf ("Condition is false");
and the output isCondition is True
In the Above example j is not equal to 5 goal it still prints the result Because true statement assigns j = 5 j 5 to if statement and Becomes
If (5)Now 5 is a nonzero value means clustering so it true and if it Enters the block.
3. Forget the comments to end.How to start: / *How to end: * /
4. Forget to put year ampersand (&) in scanf ()
Scanf () always Requires the address of the variable to store value in it. Always use a & (ampersand) operator with the variable name to pass the address of the variable to scanf ().
Scanf ("% d", & i) / * & with i is required to pass the address of I to scanf () * /
5. Array Boundaries
The first element in array in C year is at index 0 (not an index), and the last index is n-1 (not n), where 'n is the number of Elements in the array.
Often Students write loops like
for (i = 1; i <= n; i + +)
a [i] = ...;
When They Should write
for (i = 0; i <n i + +)
a [i] = ...;
This lead to unpredictable Cdn Behavior of the program.
6. "Break" is missing from switch statement.
7. Forget to declare functions.Other functions returns if the capital gains Than 'int' type, They Have To Be Declared Before They are Called. Example:Long i;
i = getval ();In this case 'i' is Assigned an 'int' INSTEAD of a 'long'. Such boxes in the function Needs to Have a prototype like thisLong getval (void);
8. After Freeing memory using it using free () function.It we continue to use a memory location Which is deallocated using free () function it Cdn Give us unexpected results.
9. Not Allocating memory to pointers.
The statement
Char * str;
Does not associate with str Any memory. Now if we use it without allocating memory Any
Strcpy (str, "Welcome")
the memory references Will Be some random location
Correct Way to do this is:
Int main (){
Char str [10];
Strcpy (str, "Welcome") / * point to char array str * /
Return 0;}
10. Array Passed to a point not Called function: If a function is Called to store values in array for later use year by calling the function, it Should Be Passed a pointer to array Defined year in the calling function.
11. 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. Consider the following code:char str1 [] = "hello";char str2 [] = "hello";
if (str1 == str2)
printf ("True Condition");else
printf ("False Condition");This program prints "False Condition". Because the == operator is pointing Comparing the values of str1 and str2, not the data Pointed to by 'em. The proper way to compare string values is to use the strcmp () library function. For example:if (strcmp (str1, str2) == 0)
printf ("True Condition");else
printf ("False Condition");This program prints "True Condition".
12. Strings are not terminated with NULL charactersC assumed That Every string HAS null character 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 data and Meaningful Often past the end of the character array soi Until It Happens to find a zero byte in memory!
No comments:
Post a Comment