Skip to content

Commit 93d78bd

Browse files
authored
Merge pull request #4294 from suhanipaliwal/suhanipaliwal-patch-2
Created errors.md
2 parents b6fee72 + b976221 commit 93d78bd

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

docs/C/errors.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
id: c-errors
3+
title: Errors in C
4+
sidebar_label: C Errors
5+
sidebar_position: 16
6+
tags: [c, c errors]
7+
description: In this tutorial, you will learn about the Errors in C, what it is.
8+
---
9+
Errors are an inherent and inevitable part of the programming process but if we are aware about the different types of errors then it can help us or effective debugging and writing a robust code. Errors in C can be broadly classified into three main categories: syntax errors, runtime errors, and logical errors.
10+
11+
### Syntax Errors
12+
13+
- Syntax errors arise during the compilation of program. If a syntax error is found, the compilation process is halted, and an error message is generated.
14+
- Some common reasons for syntax errors are as follows:
15+
- Missing Semicolons: Forgetting to end a statement with a semicolon (;).
16+
- Mismatched Braces: Incorrectly pairing parentheses () or braces {}.
17+
- Incorrect Keyword Usage: Using incorrect or misspelled keywords.
18+
- Improper Declaration: Failing to declare variables or functions correctly.
19+
- Example:
20+
```c
21+
int main() {
22+
printf("Hello, World!") // Missing semicolon
23+
return 0;
24+
}
25+
```
26+
27+
### Runtime Errors
28+
29+
- Runtime errors occur during the execution of the program. These errors are not detected by the compiler, as they depend on the program's runtime environment and input data. Runtime errors lead to incorrect results.
30+
- Some common reasons for runtime errors are as follows:
31+
- Division by Zero: Attempting to divide a number by zero, which is mathematically undefined.
32+
- Trying to open a file which has not been created.
33+
- Null Pointer Dereferencing: Accessing memory through a pointer that has not been initialized or has been set to NULL.
34+
- Lack of free memory space
35+
- Array Index Out of Bounds: Accessing an array element outside its defined range.
36+
- Example:
37+
```c
38+
int a = 10;
39+
int b = 0;
40+
int result = a / b; // Division by zero, causing a runtime error
41+
```
42+
43+
### Logical Errors
44+
45+
- Logical errors occur when a program compiles and runs without crashing, but produces incorrect or undesired output.
46+
- Some common reasons for logical errors are as follows:
47+
- Misplaced Parentheses
48+
- Incorrect Use of Conditional Statements
49+
- Floating-Point Precision Issues
50+
- Incorrect Boolean Logic
51+
- Example:
52+
```c
53+
int a = 10, b = 5, c = 2;
54+
int result = a + b * c; // Expected result might be 30, but it's actually 20
55+
```
56+

0 commit comments

Comments
 (0)