A L G O R I T H M
• Algorithm to swap two numbers.
1
Hint
a=a+b
b=a-b
a=a-b
V I S U A L I Z A T I O N T O O L
• https://pythontutor.com/visualize.html
• Game with scratch tool
• ScratchTool - https://scratch.mit.edu/projects/editor/?tutorial=getStarted
• Reference link
• https://www.youtube.com/watch?v=eAJ3AS76ZLo
2
PROGRAMM
ING IN C
TYPES OF ERRORS
• There are 5 types of error in C:
• Syntax Errors
• Runtime Errors
• Logical Errors
• Linked Errors
• Semantic Errors
4
SYNTAX ERRORS
 Syntax errors are also known as the compilation errors as
they occurred at the compilation time, or we can say that the
syntax errors are thrown by the compilers.
These errors are mainly occurred due to the mistakes while typing
or do not follow the syntax of the specified programming language.
These mistakes are generally made by beginners only because they
are new to the language.
These errors can be easily debugged or corrected. 5
C O M P I L E R A N D A S S E M B L E R
6
SYNTAX ERRORS
• #include <stdio.h>
• int main()
• {
• a = 10;
• printf("The value of a is : %d", a);
• return 0;
• }
7
RUNTIME ERRORS
Sometimes the errors exist during the execution-time even
after the successful compilation known as run-time errors.
When the program is running, and it is not able to
perform the operation is the main cause of the run-time
error.
The division by zero is the common example of the run-
time error.
These errors are very difficult to find, as the compiler does
not point to these errors. 8
RUNTIME ERRORS
#include <stdio.h>
int main()
{
int array[5];
printf("%d", array[10]);
return 0;
}
9
Some common examples of runtime
errors are:
• Division by zero
• Null pointer dereferencing
• Array index out of bounds
• Resource leaks (e.g., memory or
open file handles)
LOGICAL ERRORS
The logical error is an error that leads to an undesired output.
 These errors produce the incorrect output, but they are error-free,
known as logical errors.
These types of mistakes are mainly done by beginners.
The occurrence of these errors mainly depends upon the logical
thinking of the developer.
If the programmers sound logically good, then there will be fewer
chances of these errors.
10
L O G I C A L E R R O R - E X A M P L E
• int main()
• {
• int a, b, c;
• a = 2;
• b = 3;
• c = a - b;
• printf("The sum of a and b is %d", c);
• return 0;
• }
11
LINKER ERRORS
12
Linker errors are mainly generated when the executable file of the
program is not created.
This can be happened either due to the wrong function prototyping or usage
of the wrong header file.
For example, the main.c file contains the sub() function whose declaration
and definition is done in some other file such as func.c.
During the compilation, the compiler finds the sub() function in func.c file,
so it generates two object files, i.e., main.o and func.o.
 At the execution time, if the definition of sub() function is not found in
the func.o file, then the linker error will be thrown.
The most common linker error that occurs is that we use Main() instead
of main().
L I N K E R E R R O R
• Linker is a program that takes the object files generated by the
compiler and combines them into a single executable file.
• Linker errors are the errors encountered when the executable file of
the code can not be generated even though the code gets compiled
successfully.
• This error is generated when a different object file is unable to link
with the main object file.
13
L I N K E R E R R O R - E X A M P L E
• #include <stdio.h>
•
• void Main() {
• int var = 10;
• printf("%d", var);
• }
• Output
• undefined reference to `main'
14
S E M A N T I C E R R O R
Errors that occur because the compiler is unable to understand the
written code are called 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.
Most commonly occurring sematic errors:
use of un-initialized variables, type compatibility, and array index out of
bounds.
15
S E M A T I C E R R O R - E X A M P L E
• #include <stdio.h>
• void main() {
• int a, b, c;
•
• a * b = c;
• // This will generate a semantic error
• }
• Output
• error: lvalue required as left operand of assignment
16
W H A T T Y P E O F E R R O R ?
• #include<stdio.h>
• int main() {
• int a, b, c;
• a = 2
• b = 3;
• c = a + b;
• printf("The sum of a and b is %d", c);
• return 0;
• }
17

Programming in C - Types of Errorss.pptx

  • 1.
    A L GO R I T H M • Algorithm to swap two numbers. 1 Hint a=a+b b=a-b a=a-b
  • 2.
    V I SU A L I Z A T I O N T O O L • https://pythontutor.com/visualize.html • Game with scratch tool • ScratchTool - https://scratch.mit.edu/projects/editor/?tutorial=getStarted • Reference link • https://www.youtube.com/watch?v=eAJ3AS76ZLo 2
  • 3.
  • 4.
    TYPES OF ERRORS •There are 5 types of error in C: • Syntax Errors • Runtime Errors • Logical Errors • Linked Errors • Semantic Errors 4
  • 5.
    SYNTAX ERRORS  Syntaxerrors are also known as the compilation errors as they occurred at the compilation time, or we can say that the syntax errors are thrown by the compilers. These errors are mainly occurred due to the mistakes while typing or do not follow the syntax of the specified programming language. These mistakes are generally made by beginners only because they are new to the language. These errors can be easily debugged or corrected. 5
  • 6.
    C O MP I L E R A N D A S S E M B L E R 6
  • 7.
    SYNTAX ERRORS • #include<stdio.h> • int main() • { • a = 10; • printf("The value of a is : %d", a); • return 0; • } 7
  • 8.
    RUNTIME ERRORS Sometimes theerrors exist during the execution-time even after the successful compilation known as run-time errors. When the program is running, and it is not able to perform the operation is the main cause of the run-time error. The division by zero is the common example of the run- time error. These errors are very difficult to find, as the compiler does not point to these errors. 8
  • 9.
    RUNTIME ERRORS #include <stdio.h> intmain() { int array[5]; printf("%d", array[10]); return 0; } 9 Some common examples of runtime errors are: • Division by zero • Null pointer dereferencing • Array index out of bounds • Resource leaks (e.g., memory or open file handles)
  • 10.
    LOGICAL ERRORS The logicalerror is an error that leads to an undesired output.  These errors produce the incorrect output, but they are error-free, known as logical errors. These types of mistakes are mainly done by beginners. The occurrence of these errors mainly depends upon the logical thinking of the developer. If the programmers sound logically good, then there will be fewer chances of these errors. 10
  • 11.
    L O GI C A L E R R O R - E X A M P L E • int main() • { • int a, b, c; • a = 2; • b = 3; • c = a - b; • printf("The sum of a and b is %d", c); • return 0; • } 11
  • 12.
    LINKER ERRORS 12 Linker errorsare mainly generated when the executable file of the program is not created. This can be happened either due to the wrong function prototyping or usage of the wrong header file. For example, the main.c file contains the sub() function whose declaration and definition is done in some other file such as func.c. During the compilation, the compiler finds the sub() function in func.c file, so it generates two object files, i.e., main.o and func.o.  At the execution time, if the definition of sub() function is not found in the func.o file, then the linker error will be thrown. The most common linker error that occurs is that we use Main() instead of main().
  • 13.
    L I NK E R E R R O R • Linker is a program that takes the object files generated by the compiler and combines them into a single executable file. • Linker errors are the errors encountered when the executable file of the code can not be generated even though the code gets compiled successfully. • This error is generated when a different object file is unable to link with the main object file. 13
  • 14.
    L I NK E R E R R O R - E X A M P L E • #include <stdio.h> • • void Main() { • int var = 10; • printf("%d", var); • } • Output • undefined reference to `main' 14
  • 15.
    S E MA N T I C E R R O R Errors that occur because the compiler is unable to understand the written code are called 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. Most commonly occurring sematic errors: use of un-initialized variables, type compatibility, and array index out of bounds. 15
  • 16.
    S E MA T I C E R R O R - E X A M P L E • #include <stdio.h> • void main() { • int a, b, c; • • a * b = c; • // This will generate a semantic error • } • Output • error: lvalue required as left operand of assignment 16
  • 17.
    W H AT T Y P E O F E R R O R ? • #include<stdio.h> • int main() { • int a, b, c; • a = 2 • b = 3; • c = a + b; • printf("The sum of a and b is %d", c); • return 0; • } 17