Compiler Errors

C compiler errors split into two main categories.

  1. Syntax Errors: something wrong with the syntax of the language itself.
  2. Semantic Errors: something wrong with the logic of the code, giving unexpected results.

Syntax Errors

Syntax errors are quite common regardless the programmer experience, we all sometimes make typos. For example instead of writing int to declare an integer, one may write it as imt and that will result in a syntax error during the compilation stage.

Example code:

#include <stdio.h>

int main() {
    imt year = 2023;
    printf("Hello, %d\n", year);
    return 0;
}

Compiling this code will result into:

➜  /coding/clion/testccc/example.c:7:5: error: use of undeclared identifier 'imt'; did you mean 'int'?
    imt year = 2023;
    ^~~
    int
1 error generated.

Carefully reading the error, we can figure out the issue. The error is telling us that on line 7 (on the source file) there is an undeclared identifier that need to be fixed. Which in our case it is the imt need to be int.

Sometimes it’s easy and quick fix, other times you may don’t get what it is exactly wrong with the syntax and you’ll need to search and read some docs, either to refresh yourself our learn something new, and that’s totally fine and healthy practice.

Semantic Errors

On the other hand we have the semantic error, which is also known as a “Logical Error”, with these type of errors the program usually compile as normal without errors, but when running the program it does not produce the expected result.

The difficulty with semantic errors is that the compiler usually doesn’t recognise them, as there are no mistakes with the code itself and the compiler doesn’t really know what is your goal of writing this program and what is the end result you expect to see.

In other way, what you input is what you see on the output. If the programmer mistakenly divide a number instead of multiplying it, then it’s the programmer mistake and the compiler does not recognise such type of logical mistakes, it will compile normally both ways.

Example code:

#include <stdio.h>

int main() {
    float width, height, area;
    width = 5.5;
    height = 2.5;
    area = width * height;
    
    printf("Area = %.2f square meter\n", area);
    
    return 0;
}

In this example we are calculating the area of a rectangle, we have the width and the height, and to find the area we apply formula: area = wh.

But if we change area = width * height; to area = width + height; then the compiler will compile both ways without errors, however the end result will by different.

Which is why we always need to test all the functions of a program and make sure they function as expected before submission. A compiled program doesn’t mean it’s free of bugs.

Compiler Warnings

A Compiler warning (not error) shows that something need attention within the code, but not to the level that prevents the program from getting compiled.

However, they should not be ignored, as they can lead to big problems down the road.

Example code:

#include <stdio.h>

int main() {
    imt year;
    printf("Hello, %d\n", year);
    return 0;
}

In this case, the variable year is being declared but isn’t initialised yet with any value.

So if we compile that, we will get the following warning:

➜  vsc_c gcc -Wall example.c -o example   
example.c:6:27: warning: variable 'year' is uninitialized when used here [-Wuninitialized]
    printf("Hello, %d\n", year);
                          ^~~~
example.c:5:13: note: initialize the variable 'year' to silence this warning
    int year;
            ^
             = 0
1 warning generated.

Which tell us exactly that the variable year isn’t initialised. Mistakes like this can lead to big problems with functionality and performance/security wise, that’s why they shouldn’t be ignored even if the program does compile.

Linker Errors

Linker errors occur during the linking phase of the compilation process when multiple source files and/or external libraries are combined to create an executable program.

For example using functions from ABC library but not including that library, this would cause a linker error.

Runtime Errors

Runtime errors happen during the execution of a program. These errors cause the program to behave unexpectedly or terminate/crash, could even crash/freeze the whole computer. Usually such bugs are either an issue dealing with accessing and reading memory, data handling…etc.

References

  1. Dan Gookin, C Programming for Dummies, 2nd edition, 2021.
  2. Jens Gustedt, Modern C, 2020.