C Errors

C Tutorial

📌 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 ErrorExampleExplanation
Syntax ErrorMissing semicolonCode format violates language rules
Semantic Errorfloat x = "Hello";Type mismatch
Linker ErrorMissing function definitionDeclared but never implemented
Runtime Errora/0Error occurs while running
Logical ErrorWrong output but no crashProgram 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 TypeOccurs When?Program Runs?
Compile-time ErrorBefore execution❌ No
Run-time ErrorDuring execution⚠️ Sometimes crashes
Logical ErrorAfter execution✔ Yes, but wrong output

You may also like...