Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Difference between Compile Time Errors and Runtime Errors in C Program
Errors in C programming are interruptions that prevent code from executing successfully. Based on when they occur, errors can be classified into two main categories: compile time errors and runtime errors.
Syntax
// Compile Time Error Example int x = 10 // Missing semicolon // Runtime Error Example int result = 10 / 0; // Division by zero
Comparison Table
| Sr. No. | Key | Compile Time Errors | Runtime Errors |
|---|---|---|---|
| 1 | Definition | Errors related to syntax or semantics detected during compilation | Errors that occur during program execution |
| 2 | Detection | Detected by compiler before execution | Detected during program execution |
| 3 | Prevention | Must be fixed before program can run | Can only be identified after execution begins |
| 4 | Examples | Missing semicolons, undeclared variables, syntax errors | Division by zero, array out of bounds, null pointer access |
Example 1: Compile Time Error
This example demonstrates a compile time error due to missing semicolon −
#include <stdio.h>
int main() {
int x = 100;
int y = 155;
// Missing semicolon will cause compile error
printf("%d<br>", x + y) // Error: missing semicolon
return 0;
}
error: expected ';' before 'return'
Example 2: Runtime Error
This example shows a runtime error caused by division by zero −
#include <stdio.h>
int main() {
int n = 9;
int divisor = 0;
// This will compile but cause runtime error
int result = n / divisor; // Division by zero
printf("Result = %d<br>", result);
return 0;
}
Floating point exception (core dumped)
Key Points
- Compile time errors prevent the program from being compiled and must be fixed before execution.
- Runtime errors occur during execution and can cause the program to crash or behave unexpectedly.
- Compile time errors are easier to debug as they provide specific error messages and line numbers.
- Runtime errors require careful testing and input validation to prevent.
Conclusion
Understanding the difference between compile time and runtime errors is crucial for C programmers. Compile time errors must be resolved before execution, while runtime errors occur during program execution and require proper error handling techniques.
Advertisements
