C Errors

📌 C Errors
In C programming, errors occur when the code violates rules of the language or tries to perform invalid operations.
Errors prevent the program from compiling or running correctly.
C errors are mainly categorized into three types:
🔴 1. Compile-time Errors
These errors occur during compilation before the program runs.
Examples:
Syntax errors
Missing semicolons
Undeclared variables
Type mismatch
Wrong format specifiers
Example:
🔍 Error: Missing semicolon after 10.
🔵 2. Run-time Errors
These errors happen while the program is running, even if the code compiles successfully.
Examples:
Division by zero
Invalid memory access
File not found
Array index out of bounds
Example:
🚫 Division by zero → program crashes.
🟢 3. Logical Errors
The program compiles and runs, but produces incorrect output due to wrong logic.
Example:
💡 Output: 2, but expected result may be 8.
⚠️ Common Types of Errors in C
| Type of Error | Example | Explanation |
|---|---|---|
| Syntax Error | Missing semicolon | Code format violates language rules |
| Semantic Error | float x = "Hello"; | Type mismatch |
| Linker Error | Missing function definition | Declared but never implemented |
| Runtime Error | a/0 | Error occurs while running |
| Logical Error | Wrong output but no crash | Program logic is incorrect |
🧪 Example Showing Multiple Errors
Issues:
❌ %f used for integer input
❌ Out-of-range array index
❌ Wrong logic accessing invalid memory
🛠️ How to Avoid Errors
✔ Use correct syntax
✔ Match data types properly
✔ Check array boundaries
✔ Validate user input
✔ Test program with multiple cases
✔ Use debugging tools (like gdb)
🏁 Summary
| Error Type | Occurs When? | Program Runs? |
|---|---|---|
| Compile-time Error | Before execution | ❌ No |
| Run-time Error | During execution | ⚠️ Sometimes crashes |
| Logical Error | After execution | ✔ Yes, but wrong output |
