C Program to Show Types of Errors
Last Updated :
08 Jul, 2022
Here we will see different types of errors using a C program. In any programming language errors are common. If we miss any syntax like parenthesis or semicolon then we get syntax errors. Apart from this we also get run time errors during the execution of code.
There are 5 types of error in C:
- Syntax Errors
- Runtime Errors
- Logical Errors
- Linked Errors
- Semantic Errors
Let's discuss each of these in detail.
1. Syntax Errors
These are also referred to as compile-time errors. These errors have occurred when the rule of C writing techniques or syntaxes has been broken. These types of errors are typically flagged by the compiler prior to compilation.
Example 1: In the below program we are getting an error because of a missing semicolon at the end of the output statement (printf()) called syntax error.
C
// C program to demonstrate
// a syntax error due to
// missing semi colon
#include <stdio.h>
// Driver code
int main()
{
// missing semicolon
printf("Geeks for geeks!")
return 0;
}
Output:
Example 2: In this case, we are getting errors because of missing parenthesis before the output statement and below the main(). This type of error is also called syntax error.
C
// C program to demonstrate
// a syntax error due to
// missing parenthesis
#include <stdio.h>
// Driver code
int main()
printf("Geeks for Geeks");
return 0;
}
Output:
2. Runtime Errors
This type of error occurs while the program is running. Because this is not a compilation error, the compilation will be completed successfully. These errors occur due to segmentation fault when a number is divided by division operator or modulo division operator.
Example: Let us consider an array of length 5 i.e. array[5], but during runtime, if we try to access 10 elements i.e array[10] then we get segmentation fault errors called runtime errors. Giving only an array length of 5
C
// C program to demonstrate
// a runtime error
#include <stdio.h>
// Driver code
int main()
{
int array[5];
printf("%d", array[10]);
return 0;
}
But in output trying to access more than 5 i.e if we try to access array[10] during runtime then the program will throw an error or will show an abnormal behavior and print any garbage value.
3. Logical Errors
Even if the syntax and other factors are correct, we may not get the desired results due to logical issues. These are referred to as logical errors. We sometimes put a semicolon after a loop, which is syntactically correct but results in one blank loop. In that case, it will display the desired output.
Example: In the below example, the for loop iterates 5 times but the output will be displayed only one time due to the semicolon at the end of for loop. This kind of error is called a logical error.
C
// C program to demonstrate
// a logical error
#include <stdio.h>
// Driver code
int main()
{
int i;
for(i = 0; i <= 5; i++);
{
printf("Geeks for Geeks");
}
return 0;
}
4. Linker Errors
When the program is successfully compiled and attempting to link the different object files with the main object file, errors will occur. When this error occurs, the executable is not generated. This could be due to incorrect function prototyping, an incorrect header file, or other factors. If main() is written as Main(), a linked error will be generated.
Example: Below is the C program to show the linker error.
C
// C program to demonstrate
// a linker error
#include <stdio.h>
// Driver code
int Main()
{
printf("Geeks for Geeks");
return 0;
}
Output:
5. Semantic Errors
When a sentence is syntactically correct but has no meaning, semantic errors occur. This is similar to grammatical errors. If an expression is entered on the left side of the assignment operator, a semantic error may occur.
Example: Below is the C program to show semantic error.
C
// C program to demonstrate
// a semantic error
#include <stdio.h>
// Driver code
int main()
{
int x = 10;
b = 20, c;
x + y = c;
printf("%d", c);
return 0;
}
Output:
Similar Reads
C Program to Show Unreachable Code Error Here, we will see how to show unreachable code errors using the C program. Unreachable statements are statements that will not be executed during the program execution. These statements may be unreachable due to the following reasons: Return statement before print statement.Infinite loop before stat
3 min read
C Program to Show Runtime Exceptions Runtime exceptions occur while the program is running. Here, the compilation process will be successful. These errors occur due to segmentation faults and when a number is divided by a division operator or modulo division operator. Types of Runtime Errors:An invalid memory access Dividing by zero. S
3 min read
Program to find out the data type of user input Take a input from user and find out the data type of input value. Examples : Input : geek Output : The input is a string Input : chetna Output : The input is a string Input : 4 Output : The input is a integer Below is C program to find the datatype of user input : C // C program to find data type #i
2 min read
C Program to Find the Size of int, float, double and char Write a C program to find the size of the data types: int, float, double, and char in bytes and print it on the output screen.ExamplesInput: charOutput: Size of char: 1 byteInput: intOutput:Size of int: 4 bytesDifferent Methods to Find the Size of int, float, double and char in CWe can find the size
4 min read
C Exercises - Practice Questions with Solutions for C Programming The best way to learn C programming language is by hands-on practice. This C Exercise page contains the top 30 C exercise questions with solutions that are designed for both beginners and advanced programmers. It covers all major concepts like arrays, pointers, for-loop, and many more.So, Keep it Up
12 min read
C Programs To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read
Error Handling During File Operations in C File operations are a common task in C programming, but they can encounter various errors that need to be handled gracefully. Proper error handling ensures that your program can handle unexpected situations, such as missing files or insufficient permissions, without crashing. In this article, we wil
5 min read
Else without IF and L-Value Required Error in C Else without IF This error is shown if we write anything in between if and else clause. Example: C #include <stdio.h> void main() { int a; if (a == 2) a++; // due to this line, we will // get error as misplaced else. printf("value of a is", a); else printf("value of a is not equ
2 min read
Interesting Facts in C Programming | Set 2 Below are some more interesting facts about C programming: 1. Macros can have unbalanced braces: When we use #define for a constant, the preprocessor produces a C program where the defined constant is searched and matching tokens are replaced with the given expression. Example:C #include <stdio.h
2 min read
C Compound Data Types Practice Problems Compound Data Types are those data types in C that are created using basic data types. They provide an interface to use the built-in data types is different ways to satisfy our requirement. They are frequently used to handle real world cases it is a must for programmers to have good clear understand
2 min read