Errors in C Language
Errors are categorized into two type:
- Warnings
- Errors
1. Warnings:
- Warnings, which indicate a potential problem, but still let your code compile.
- Warnings are sort of future bugs that will appear at runtime i.e. We might get undesired output even the inputs and the code are properly crafted.
- The modern compilers mostly generate the warnings and still compile your code till the executable file; the modern compilers automatically detect the problems and solve it on its own if it is small.
Example: In this example below,
we haven’t included the string library and still using string based predefined
function in the program; the program will execute in this case but warning will
come as “include <string.h> or provide a declaration for strlen ()”
To avoid warning: You must include proper header files
in the program.
In above case, include string-based functions header
file i.e. Write #include<string.h> before main ().
2. Errors:
Types of errors we are going to discuss are:
- Compile Time errors
- Runtime errors
Compile time errors are generated by compilers at the
time of compiling the c program whereas, runtime errors are generated at runtime
or while executing the code by OS.
Compile time errors are further categorized into four:
- Compile time Pre-processor error
- Compile time Translator error
- Compile time Assembler error
- Compile time Linker error
Compile time Pre-processor error:
If there is a syntactical error in pre-processor
directives or while including library files the ‘fatal error’ appears at the
first stage of compilation.
Instead of #include<stdio.h>; included <stdi.h>(missing
‘o’) intentionally to create error.
Compile time Translator error:
If there is a syntactical error in whole program like
somewhere parentheses, semicolon is missing or some of the brackets are misplaced
or the colon (:) is added instead of semi colon (;); the error generates in the
second stage of compilation.
Compile time Assembler error:
There is no chance of getting this type of error
unless you corrupt .s file intentionally and then compile that corrupted file
ton generate opt code file (. o).
It generates error as “file corrupt”
Corrupting .o file intentionally by writing “ABCD”
Compile time Linker error:
If there is no reference to the functions or libraries
while writing functions, this error will appear as “unknown/undefined reference”
Example: if we have called the user defined function from
main () and it is not defined in our c program it will generate “undefined
reference to...” message on the compilation screen.
2. Runtime Errors:
These are the errors comes at the time of execution.
The compiler will compile the code normally without
any errors and will create a.out or .exe file but at runtime the errors will appear;
these are generally logical error or human errors.
Case 1:
Case 2:
In Case 1, there’s a logical error; assignment operator is used instead of comparison operator; even if a value is 10, the if condition will assign a value as 9 and print “EQUAL” as output.
In Case 2, it is corrected.
Hope it helps.
Do follow us on
Instagram: @coding_over_chai
Blog: https://codeoverchai.blogspot.com/
Comments
Post a Comment