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.
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:
A runtime error is a program error that occurs while the program is running. Some examples of run-time errors are:
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.
A logical error is a bug in a program that causes it to operate incorrectly but not to terminate abnormally.
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.
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;
}
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
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.