C Programming Errors and Solutions
Syntax errors, runtime errors, and logic errors are the three main categories of problems in C programming. When a piece of code deviates from the syntax of the C language, such as by omitting semicolons or using the wrong variable declarations, a syntax error occurs. Runtime errors, such as null pointer errors or memory access violations, happen while a program is being executed and can cause crashes or unexpected behaviour. When the algorithm or logic in the program is flawed, the program cannot generate the desired result. Then, it is referred to as logical errors. It is essential to thoroughly examine, debug, and test C code to find flaws and fix them for reliable and precise programme operation.
Let’s discuss about each type of error in detail.
Syntax Errors
Syntax errors in C refer to mistakes in the code that violate the rules and structure of the C language. These errors prevent the compiler from interpreting the code correctly and result in compilation failures.
Let’s look into some common syntax errors with examples:
Missing Semicolon
#include <stdio.h>
int main() {
int num = 10
printf("Number: %d\n", num);
return 0;
}
When we observe the code above, we see that there an error exists. The error is missing semicolon after variable declaration. Let’s see how to fix it?
Solution
To fix it, you can add semicolon after variable declaration, such as:
#include <stdio.h>
int main() {
int num = 10;
printf("Number: %d\n", num);
return 0;
}
Output:
Number: 10
Mismatched Parentheses
Observe the code below:
#include <stdio.h>
int main() {
int num = 10;
printf("Number: %d\n", num;
return 0;
}
Can you figure out the error? Here the error is the missing closing parenthesis in the printf statement.
Solution
To fix this issue, we can add a closing parenthesis in the printf statement, as shown below:
#include <stdio.h>
int main() {
int num = 10;
printf("Number: %d\n", num);
return 0;
}
Output:
Number: 10
Incorrect Function Call
Observe the code below and figure out the error.
#include <stdio.h>
int main() {
print("Hello, world!\n");
return 0;
}
Here you can see the error is incorrect function name in the printf statement.
Solution
We can fix it by using the correct function name in the printf statement, which is "printf" in this case.
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
Output:
Hello, world!
Misspelled Keyword
Observe the code given below and find out the error here:
#incldue <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
If you observe closely you will see that here we have misspelled "include" keyword.
Solution
We can fix it by correcting the spelling of the "include" keyword.
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
Output:
Hello, world!
Runtime Errors
Runtime errors in C occur during the execution of a program and can result in abnormal termination or unexpected behaviour. These errors are typically caused by incorrect use of pointers, improper memory access, division by zero, or other issues that occur at runtime.
Let’s look into some common runtime errors with examples
Null Pointer Dereference
Observe the code below, and figure out the error:
#include <stdio.h>
int main() {
int* ptr = NULL;
printf("%d\n", *ptr);
return 0;
}
Here the error is we are Dereferencing a null pointer.
Solution
We can fix it by initialising the pointer to a valid memory address before dereferencing it.
Array Out of Bounds
Observe the code below, and figure out the error:
#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
printf("%d\n", arr[5]);
return 0;
}
Here the error is we are trying to accessing an array out of bounds. How to fix this?
Solution
We can fix this by ensuring that the array index is within the valid range of array elements, as shown below:
#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
printf("%d\n", arr[2]);
return 0;
}
Division by Zero
Observe the code below, and figure out the error:
#include <stdio.h>
int main() {
int num = 10;
int denom = 0;
int result = num / denom;
printf("%d\n", result);
return 0;
}
Here the error is division by zero.
Solution
We can fix this by avoiding division by zero and handling such cases in the code using appropriate error checking mechanisms.
#include <stdio.h>
int main() {
int num = 10;
int denom = 2;
int result = 0;
if (denom != 0) {
result = num / denom;
printf("%d\n", result);
} else {
printf("Error: Division by zero\n");
}
return 0;
}
Memory Leak
Observe the code below, and figure out the error:
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr = (int*)malloc(sizeof(int));
// ... do some operations
return 0;
}
The above code will result in error as we are not freeing the dynamically allocated memory, resulting in memory leak.
Solution
To avoid this error, we can free the dynamically allocated memory using the free() function before the program exits, as shown below:
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr = (int*)malloc(sizeof(int));
// ... do some operations
free(ptr);
return 0;
}
Buffer Overflow
Observe the given code, and try to understand the error:
#include <stdio.h>
int main() {
char buffer[5];
strcpy(buffer, "Overflow!"); // copying more than 5 characters in the buffer
printf("%s", buffer);
return 0;
}
The above code gives error during runtime of the code. As we are copying more than five characters in the character array of size five in the above case, which results in a runtime error of buffer overflow.
Solution
We can fix this error by increasing the length of character. Solution after increasing the length of array:
#include <stdio.h>
int main() {
char buffer[100]; //increase the length of array
strcpy(buffer, "Overflow!");
printf("%s", buffer);
return 0;
}
Use After Free
Look into the code below and try to find the error:
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr = (int*)malloc(sizeof(int)); // Allocating memory
*ptr = 10;
free(ptr); // Freeing the memory
printf("%d", *ptr); // Using after free the memory
return 0;
}
This code gives run time error, because we are accessing the pointer after the memory it points to has been freed. Such errors are also a type of run time error.
Solution
To solve this error, we have to use and access the pointer before freeing its memory. Here is the solution code for such error.
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr = (int*)malloc(sizeof(int)); // Allocating memory
*ptr = 10;
printf("%d", *ptr);
free(ptr); // free after accessing the pointer variable
return 0;
}
Logical Errors
A logical error is a sort of programming error that affects programme behaviour but does not result in a syntax or runtime error. When an algorithmic fault or improper application of programming constructs causes the code to fail to achieve the desired result, this is known as a logical error.
Let’s look into some of the common logical errors:
- Incorrect conditional logic
Look into the code below, can you figure out any errors here:
#include <stdio.h>
int main() {
int age = 25;
if (age >= 18 && age <= 60) { // Incorrect condition
printf("You are an adult.\n");
} else {
printf("You are not an adult.\n");
}
return 0;
}
Here, there are no syntax error or compilation error rather the code has wrong logic in the if condition. How will you fix it?
Solution
We can fix this code by making changes in the if condition as shown below:
#include <stdio.h>
int main() {
int age = 25;
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are not an adult.\n");
}
return 0;
}
- Incorrect Loop Logic
Observe the code below and try to find the errors:
#include <stdio.h>
int main() {
int i;
for (i = 1; i<= 10; i--) { // Incorrect loop logic
printf("%d\n", i);
}
return 0;
}
If you observe the for loop closely, you will see that the decrement operator should not have been used here. How will you fix it?
Solution
In order to fix the above code, we will replace the decrement operator with the increment operator as shown in the code below:
#include <stdio.h>
int main() {
int i;
for (i = 1; i<= 10; i++) {
printf("%d\n", i);
}
return 0;
}
- Incorrect operator assignment
Observe the given code, and try to understand the error:
#include <stdio.h>
int main() {
int length = 10;
int breadth = 5;
int area = length + breadth; // Incorrect operator assignment
printf("Area: %d\n", area);
return 0;
}
If you observe the code, you will see that the code is written for the area in which we have to multiply the length and breadth but it has sum operator which is error in this code.
Solution
We can fix this code by using multiply operator in place of sum operator as shown in the code below:
#include <stdio.h>
int main() {
int length = 10;
int breadth = 5;
int area = length * breadth;
printf("Area: %d\n", area);
return 0;
}
- Incorrect Conditionals
Observe the given code. Can you find the error in the code?
#include <stdio.h>
int main() {
int x = 5;
if (x = 10) { // Incorrect conditional
printf("x is 10\n");
}
else{
printf("x is not 10\n");
}
return 0;
}
In the above code, the condition in written wrong which is not caught by any syntax and runtime compiler. In every case it will print "x is 10" which is not logical correct. How you can fix it?
Solution
We can fix this error by using the equal-to operator ( == )in the condition statement as shown in the code below:
#include <stdio.h>
int main() {
int x = 5;
if (x == 10) {// Incorrect conditional
printf("x is 10\n");
}
else{
printf("x is not 10\n");
}
return 0;
}
Conclusion
Different types of errors occur while you code in C programming language. In this article, we get to know about the different types of error in c programming. We see different types of Syntax, Runtime, and Logical errors in detail and learn how to fix those errors.