Fundamental of Programming in C Errors - 02
Fundamental of Programming in C Errors - 02
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.
#include <stdio.h>
// Driver code
int main()
int x = 10;
b = 20, c;
x + y = c;
printf("%d", c);
return 0;
Output: