0% found this document useful (0 votes)
12 views

Fundamental of Programming in C Errors - 02

The document discusses different types of errors in C programming, including syntax errors, runtime errors, logical errors, linker errors, and semantic errors. Each error type is explained with examples demonstrating how they occur and their implications. The document serves as a guide for BCA first semester students to understand common programming pitfalls in C.

Uploaded by

korimy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Fundamental of Programming in C Errors - 02

The document discusses different types of errors in C programming, including syntax errors, runtime errors, logical errors, linker errors, and semantic errors. Each error type is explained with examples demonstrating how they occur and their implications. The document serves as a guide for BCA first semester students to understand common programming pitfalls in C.

Uploaded by

korimy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Fundamental of Programming in C

BCA FIRST Semester


Notes- 02
C Program to Show Types of Errors
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:
1. Syntax Errors
2. Runtime Errors
3. Logical Errors
4. Linked Errors
5. Semantic Errors
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 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;
}
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 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 program to demonstrate a runtime error
#include <stdio.h>
// Driver code
int main()
{
int array[5];
printf("%d", array[10]);
return 0;
}

Output
-621007737

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 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;
}
Output
Geeks for Geeks

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 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 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:

You might also like