For working professionals
For fresh graduates
More
Explore C Tutorials: From Begi…
1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
Now Reading
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
Errors in C programming can disrupt the intended functionality of a program, causing issues such as failed compilation, program crashes, or incorrect output. There are several common types of errors:
Each error type carries its own implications, and understanding them is crucial for effective debugging.
In this article, we will explore and provide examples for each type of error, shedding light on their significance in the C programming language.
To address an error in our code, it is crucial to understand its cause and occurrence. When encountering an error, the compiler halts the compilation process if it detects a syntax error. The specific line of code responsible for the error is usually highlighted in such cases. The root cause of the error can often be identified on the highlighted line or in the code above it.
#include <stdio.h> |
In this example, a syntax error occurs due to the missing semicolon (;) at the end of the printf statement. The compiler would highlight the line and display an error message indicating the syntax error.
#include <stdio.h> |
A run-time error occurs in this case because we attempt to divide an integer by zero. During program execution, a run-time error is detected, causing the program to terminate abruptly. The division by zero is not allowed and leads to undefined behaviour.
#include <stdio.h> |
Here, a logical error arises due to an incorrect formula for calculating the area of a circle. The formula should be pi * radius * radius, but the programmer mistakenly uses pi * radius instead. As a result, the program will produce incorrect output for the area.
Here is a list of types of errors in c with examples -
Syntax error in C occurs when the code violates the rules and structure of the programming language. These errors prevent the code from being compiled and executed. The compiler identifies syntax errors and displays error messages pointing to the specific line or lines where the error occurred.
Examples -
#include <stdio.h> |
In this example, a semicolon is missing at the end of the printf statement. The compiler will display an error message indicating the missing semicolon.
#include <stdio.h> |
In this example, there is a mismatched bracket in the if statement. The closing bracket for the if block is missing before the else statement. The compiler will detect this syntax error and provide an error message.
Missing Brackets Error Example
Runtime error in C, also known as exceptions or execution errors, occur during the execution of a program. Unlike syntax errors, runtime errors do not prevent the code from being compiled, but they cause the program to behave unexpectedly, crash, or produce incorrect output. Runtime errors are typically caused by invalid input, zero division, accessing out-of-bounds memory, or incompatible data types.
#include <stdio.h> |
In this example, a runtime error occurs because we are attempting to divide an integer by zero. During program execution, a runtime error is detected, and the program terminates abruptly.
Division by Zero Runtime Error
#include <stdio.h> |
Null Pointer Deference Runtime Error Example
Logical error in C, also known as semantic errors, occur when the program runs without any syntax or runtime errors but produces incorrect or unexpected results. These errors are caused by flawed logic or incorrect algorithmic implementation in the code.
Unlike syntax or runtime errors, logical errors do not generate error messages or warnings from the compiler or runtime system. Detecting and fixing logical errors requires careful analysis of the code's logic and understanding of the intended behaviour.
a = 5 |
In this example, the sequence of statements is incorrect. The value of a is printed before it is updated to b, resulting in the output being 5 instead of 3.
x = 10 |
In this example, the Boolean expression used in the if statement is incorrect. Instead of checking for equality between x and y, it checks for inequality. This leads to the incorrect output "x is not equal to y" even when x and y are equal.
Linker errors occur during the linking phase of the program's compilation process. The linker resolves external references, combines multiple object files, and generates the final executable file. Linker errors occur when there are issues with linking the object files together to create the executable.
main.obj : error LNK2019: unresolved external symbol addNumbers referenced in function main |
In this example, the linker error indicates that the symbol addNumbers is referenced in the main function but is not defined or implemented in any object files. It suggests that the function addNumbers needs to be defined or adequately linked.
first.obj : error LNK2005: _variableName already defined in second.obj |
This linker error occurs when the symbol _variableName is defined in both first.obj and second.obj. It indicates a conflict due to duplicate definitions and suggests resolving the issue by ensuring unique symbol names.
Preprocessor errors occur during the preprocessing phase of the compilation process, where the preprocessor directives are processed before the actual compilation begins. These errors are related to the preprocessing directives and can prevent the code from being properly processed and compiled.
If a required header file is not included or the file path is incorrect, it can cause a preprocessor error. For example,
#include <stdio.h> // Correct header file inclusion |
When two header files include each other, it leads to a circular dependency and preprocessor error. For example,
// file1.h |
Debugging tools are essential for identifying and resolving errors. Integrated Development Environments (IDEs) often provide debugging features such as breakpoints, step-through execution, variable inspection, and call stack tracing. By leveraging these tools, you can
Tracing the code involves manually examining the code's execution path to identify the point where the error occurs. This can be done by strategically inserting logs or printing statements at different parts of the code to display intermediate values or checkpoints. Observing the output or logs lets you
Print statements, also known as "printf" statements in languages like C/C++, are simple yet effective for debugging. By selectively adding print statements at key points in the code, you can output variable values or custom messages to the console or log files. This lets you observe the program's state during execution and locate the error.
Understanding the different types of errors in C programming is crucial for developing reliable software. Programmers can create robust and efficient C programs by adopting good coding practices and mastering error handling. As mentioned in the above article, the right guidance and pointers can help any programmer, fresher or seasoned, easily deal with any type of error.
Wondering how you can further strengthen your programming abilities?
Check out upGrad’s Executive Post Graduate Programme in Machine Learning & AI - Now with Generative AI lectures course that can help students polish their programming skills and acquire mastery across in-demand topics such as Dall-E, ChatGPT, Graph Neural Network or GNN, and machine learning. This course provides a deeply immersive learning experience to help students advance in their careers and showcase positive future growth.
Don’t miss this opportunity, enroll now!
1. How many types of syntax errors are there?
There are various syntax errors, such as missing semicolons, mismatched parentheses, and undefined variables.
2. What is a lexical error?
A lexical error occurs when the compiler encounters an illegal sequence of characters or tokens in the source code.
3. Is a syntax error a bug?
Syntax errors are not considered bugs. They are mistakes in the code that violate the language's syntax rules, resulting in compilation failures.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.