Do you know what are errors in C programming? Have you ever thought about how to overcome the errors in C programming? Today we are going to experience how to handle errors in C programming.
But before we start the programming journey, let’s think about what are errors generally.
Errors are mistakes that humans generally made in their life. In common life also, in every step of daily life individuals commit some type of mistakes. But mistakes play an important role in life. Mistakes or errors are the best teachers in our life. Humans gain experience from the mistakes they have made earlier. Hence, they try not to make such mistakes again in life.
The same thing goes for programming languages also. In every programming language, there are certain types of errors present. These errors help to write the code in a prescribed way. While writing a piece of code, programmers may create some type of error. Then the compiler will aware the programmer about the error. Hence, the programmer can resolve the error.
But what is the need to learn about different error categories?
Let’s assume one scenario:
Suppose, as a programmer, you are writing a code. At any point in time compiler is aware that you have made one mistake. But you can’t identify from the compiler what type of mistake you have done. As you don’t know the categories of the errors. Hence, your program gets stuck.
That is why it is needed to know the categories of errors.
But is there any way to know about the errors in C programming? Yes! There are.
What Are Errors in C Programming? Get To Know More
Errors are like the illegal type of expression written by the programmer. Every programming language like C can able to read (or compile) such types of codes that are written in a given format. If the programmer by any chance writes the code in a different format, then the compiler will throw an error. These errors will guide the programmer to resolve the problem which happened in a particular line of the code.
It is very obvious that if you only know the English language & some person is talking with you in the Bengali language. You can’t able to know what he is talking about. The same thing goes for compiler also. They can only know a particular language & grammar format. Writing differently will cause an error.
Types of Errors in C Programming:
In the C programming language, there are mainly four types of errors present. Depending upon the mistakes made by the programmers these categories have been created. Let’s make a list of those errors:
- Compile Time Error
- Run Time Error
- Linker Error
- Logical Error
Let’s know each of the errors one by one in a detailed way.
Compile Time Errors in C Programming:
As the title says that this type of error occurs when the code is compiling. After compilation of any code that code runs.
In this type of error, programmers generally made mistakes in the syntax structure. This means they forget to provide the end braces (‘}’), forget to provide the semicolons, etc. Sometimes, syntax errors related to the loops can occur such problems.
These errors should be fixed before the code runs. If these errors are present in the code, it restricts the compilation of the code.
Let’s take an example where a syntax error problem happened to for loop.
General Syntax of For Loop: for(statement;condition;updatation)
Example:
// Declaring The Header File #include <stdio.h> // Calling main Function void main() { // Declaring The Variable int i; // For Loop Defect-Comma Instead of Semicolon for(i=0;i<10,i++) { // Simple Statement printf("Hello C Language"); } }
Let’s look at the output of the above code. Hence, we come to know about the Complier time errors in C Programming
Output:
Runtime Errors in C Programming:
As the title says, this type of error occurs when the compiler tries to run the code & provide the output. This means this type of error successfully skips the compilation step. But it gets stuck while running the code.
This type of error includes arithmetical problems, declaration problems, etc. This means, that if you declared a variable as Integer & trying to a large value user input from there it will prompt an error. Also, if you declare a variable as Integer & working with it as a Float, then it will provide a run time error.
This type of error can successfully pass through the compile time as the programmers have written them correctly. Their syntax is correct but their work is not applicable.
Let’s take an example where we try to divide one number using zero. This type of code will prompt a run time error. As the code has been written correctly given syntax, that is why it will not prompt compile time error.
Example:
// Declaring The Header File #include <stdio.h> // Calling main Function void main() { // Declaring The Variable int num = 5; // Run Time Error-Trying To Divide by Zero printf("result = %d", num/0); }
Let’s look at the output of the above code. Hence, we come to know about the Runtime errors in C Programming.
Output:
Linker Errors in C Programming:
This type of error happens when the linking process is being performed by the compiler. The linking process is a very important & very beginning process. Before compilation linking is done. In this process, the code is converted to the linker file which helps to compile & run the code.
This type of error occurs when programmers write illegal header files & main functions. Header files & main functions play a crucial role in the lining. So, making mistakes while writing those, will create the linker error.
Let’s take an example where the main function is written in the wrong way
Example:
// Declaring The Header File #include <stdio.h> // Calling Wrong main Function void mainn() { // Simple Statement printf("Hello C Program"); }
Let’s look at the output of the above code. Hence, we come to know about the Linker errors in C Programming.
Output:
Logical Errors in C Programming:
This type of error occurs when the programmer writes something unknowingly. This type of problem is faced by beginners. Sometimes they write in such a format that desired output not even appeared on the console. Though the compiler is not even aware of any error. Then the programmer completely must go through the code & find out the mistake.
But sometimes, for some mistakes complier prompt an error. Here, we have taken such an example where an incorrectly written assignment operation will prompt an error.
Correct Syntax: variable1= variable2 + variable3
Example:
// Declaring The Header File #include <stdio.h> // Calling main Function void main() { // Declaring The Variables int x, y, z; // Wrong Syntax For Assignment Operation x + y = z; }
Let’s look at the output of the above code. Hence, we come to know about the Logical errors in C Programming.
Output:
Conclusion:
As we saw errors perform an important role.
It helps to take experiences & help us not to make such mistakes in future.
Errors perform a special role when developers write some big-size codes to perform a bigger output. It helps to guide their mistakes.
So, hope you have liked this piece of article. Share your thoughts in the comments section and let us know if we can improve more.
Codingzap also offers a wide range of programming and coding help services for you to benefit from. Don’t miss out! Visit https://codingzap.com/ and our expert team will be there to help you out.
If you want to know more about ‘Dynamic Memory Allocation in C’ then you can check out our article.