💡Beginner

Show Types of Errors

Different error types

Learn about different types of errors in C: Syntax, Runtime, and Logical errors.

Types of Errors

1. Syntax Errors (Compile-time)

Errors detected by the compiler due to incorrect syntax.

syntax_error_example.c
C
1// Missing semicolon
2int x = 5 // Error: expected ';'
3
4// Undeclared variable
5y = 10; // Error: 'y' undeclared
6
7// Mismatched brackets
8if (x > 5) {
9 printf("Greater");
10// Error: missing closing brace

2. Runtime Errors

Errors that occur during program execution.

runtime_error_example.c
C
1#include <stdio.h>
2
3int main() {
4 int a = 10, b = 0;
5
6 // Division by zero - Runtime error
7 int result = a / b; // Crash!
8
9 // Array out of bounds
10 int arr[5];
11 arr[10] = 100; // Undefined behavior
12
13 return 0;
14}

3. Logical Errors

Program runs but gives incorrect results.

logical_error_example.c
C
1#include <stdio.h>
2
3int main() {
4 int sum = 0;
5
6 // Logical error: wrong loop condition
7 for (int i = 1; i < 5; i++) { // Should be <= 5
8 sum += i;
9 }
10
11 printf("Sum of 1-5: %d\n", sum); // Prints 10, not 15
12
13 return 0;
14}

Want to Learn More?

Explore our comprehensive tutorials for in-depth explanations of C programming concepts.

Browse Tutorials
Back to All Examples