Common errors in C programming | Faceprep

Common errors in C programming | Faceprep

In this article, we will discuss some of the most common errors in C/C++ programming made by a lot of students. Learning about these errors will save a lot of time while programming.


Common errors in C programming

Types of Errors in Programming

SYNTAX ERROR:

A syntax error is an error in the syntax of a coding or programming language, entered by a programmer. Syntax errors occur when there is a violation of syntaxes followed in C/C++ Programming. These syntax errors are caught by the compiler and hence are called as compiler-time errors also. Some syntax errors in C/C++ Programming are:

  • Misspelt variable and function names
  • Incomplete braces
  • Missing semicolons

RUNTIME ERROR:

A runtime error is a program error that occurs while the program is running. Some examples of run-time errors are:

  • Trying to divide by a variable that contains a value of zero
  • Trying to open a file that doesn’t exist

LINKER ERRORS:

If you receive a linker error, it means that your code compiles fine, but that some function or library that is needed cannot be found.

LOGICAL ERRORS:

A logical error is a bug in a program that causes it to operate incorrectly but not to terminate abnormally. 

SEMANTIC ERRORS:

A semantic error is also called a “logic error;” however, some programmers believe a logic error produces erroneous data, whereas a semantic error yields nothing meaningful at all.


Common errors in C programming

Example of Common Errors in C Programming


a) Undeclared variables:


int main()n{n  cin>>x;n  cout<<x;n}n


Your compiler does not know what x means. You need to declare it as a variable.


Correct answer:


int main()n{n  int x;n  cin>>x;n  cout<<x;n}n

 

b) Uninitialized variables: 


Remember to initialize your variables before using them in the program.

int count;

while(count<100)

{

 cout<<count;

 count++;

}

 

c) Incomplete/Wrong usage of operators: Using a single equal sign to check your equality:


char x='Y';nwhile(x='Y')n{n  //...n  cout<<"Continue? (Y/N)";n  cin>>x;n}n


Correct answer:


char x=’Y’;

while(‘Y’==x)

{

 //…

 cout<<“Continue? (Y/N)”;

 cin>>x;

}

 


Common errors in C programming

d) Undeclared functions: The function should be declared above the main function in c programming.


int main()n{n  menu();n}nvoid menu()n{n  //...n}n


Correct answer:


void menu();nint main()n{n  menu();n}nvoid menu()n{n  ...n}n

 

e) Termination of statements:


int x;nfor(x=0; x<100; x++);n  cout<<x;n


Semicolons don’t go after if statements, for loop and function definitions. Hence the right way of writing the above program is


int x;nfor(x=0; x<100; x++)n  cout<<x;n

 

f) Overstepping array boundaries:


int array[10];n//...nfor(int x=1; x<=10; x++)n  cout<<array[x];n


Correct answer:


int array[10];n//...nfor(int x=0; x<10; x++)n  cout<<array[x];n

 

g) Incomplete loops


for(i=1;i<=10;i++)n               sum1=sum1+i;n               sum2=sum2+i*i;nprintf(“%d%dn”,sum1,sum2);n


Correct answer:


for(i=1;i<=10;i++) {n               sum1=sum1+i;n               sum2=sum2+i*i;n}nprintf(“%d%dn”,sum1,sum2);n

 

h) Improper usage of “” and ” : “” is used for declaring a string whereas ” is used for initializing a character.


if(response==YES) n


Correct answer:


if(response==”YES”)n

 

i) Forgetting/Ignoring the precedence of operators


if(value = product() >= 100)n               tax=0.05*value;n


Correct answer:


if((value = product()) >= 100)n               tax=0.05*value;n

 

j) Missing & operators in scanf operator

k) Infinite loops:


int k=3;nwhile (k==3)n{nPrintf("%d", k);n}n



Common errors in C programming

The above program leads to an infinite loop and continuously prints the value of K. This infinite loop can be avoided by writing this way.


int k=3;nwhile (k==3)n{nPrintf("%d", k);nk++;n}n

 

Now that you have learnt about some of the most commonly made mistakes while programming in C/C++, it is good to practice a few problems. Click here to Practice most asked C programming questions.



Common errors in C programming

c