Tutorial Playlist
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.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...