Pages

Sunday, May 7, 2017

Top 10 errors

1. Undeclared Variables

Example:

int sum;
sum = num1 + num2;

Explanation of error: num1 and num2 variables are not declared, but used in statement, hence error at line sum = num1 + num2;.

2. Use of Uninitialized variables

Example:

int num1=5, total, answer;
answer = num1 + total;

Explanation of error: There is no value initialized in total variable which result in miscellaneous answer.

3. Using a single equal sign to check equality

Example:

if(num = 5)
      printf("Five");



Explanation of error: Single (=) is assignment operator. Correct way to compare is use of double equal sign(==).

4. Undeclared Functions


5. Extra Semicolons – at the end of loop

Example:

for(int i=0; i<10; i++);
     printf("%d", i);

Explanation of error: Semi-colon at the end of for loop will ends for statement. Hence printf will not be executed 10 times. It will be executed only once.

6. Accessing elements out of Array boundaries

Example:



int number[10];
for(int i=1; i<=10; i++)
     printf(“%d”, number[i]);       


Explanation of error: Valid index range for number array is 0 to 9. This for loop generates index number 1 to 10, which will results in unexpected result.  

7. Inappropriate use of Integer variable



Example:

int percentage;
percentage = 75.50;



Explanation of error: Assigning floating point value to integer variable results in unexpected result.

8. scanf() errors



Example:

int number;
scanf(“%d”, number);

Explanation of error: Address of operator (&) is missing in scanf results miscellaneous output.

9. String error

Example:

char name[20];
scanf(“%s”, name);

Explanation of error: User will be able to enter only one word using this scanf. If user enters FirstName and LastName, then second word in the input will be ignored.

10. Returning no value from non-void type of function.

Example:

int main()
{
   printf("Hello India");
}

Explanation of error: return 0;  before the last curly bracket is must as return type of main( ) is int.


Click here to read more details.

No comments:

Post a Comment