Common clang
Errors and Warnings
Here is the list of common clang
errors and warnings that you may encounter. We will expand this list over the semester.
How to Read the Messages
clang
messages always start with the name of the file, the line number, and the character position. For instance,
1 |
|
shows the error in the file error.c
, line 3, position 5.
In vim
, you can use :<line number>
to jump directly to the line containing this error.
Variables
Error: Use of undeclared identifier
C is a static type language, and thus every variable used must be declared with its type.
E.g.,
1 2 3 4 |
|
would lead to the error
1 2 3 |
|
In the example above, x
is used by not declared. To fix, declare x
with its type.
E.g.,
1 2 3 4 |
|
Error: Redefinition of a variable
Each variable should be declared exactly once within its scope (scoped by {
and }
).
E.g.,
1 2 3 4 5 |
|
Would give the error
1 2 3 4 5 6 |
|
To fix, check whether you intend the second declaration to be the same variable (in which case, remove the declaration) or a new one (in which case, give it a different name).
Warning: Unused Variable
Declaring variables that are not used clutters the code. It is a good programming practice to only declare the variables that you need. CS1010 insists on this. If you declare variables that you end up not using, you will be penalized.
1 2 3 4 |
|
1 2 3 |
|
To fix, go through all such warnings and remove any variables that you declared/initialized but never used.
Warning: Variable May Be Uninitialized
A variable is uninitialized if it is declared but not assigned any value. This might lead to bugs in your code.
1 2 3 4 5 |
|
1 2 3 4 5 6 7 |
|
To fix, initialize the variable to the appropriate value.
Warning: Declarations shadows a local variable.
Avoid naming a variable the same name as another variable in the outer scope. Doing so makes your code confusing to read. E.g.,
1 2 3 4 5 6 |
|
1 2 3 4 5 6 |
|
Warning: No previous extern declaration for non-static variable
A global variable is detected. The use of global variables is bug-prone and should be avoided. For instance,
1 2 3 4 |
|
1 2 3 4 5 6 7 8 |
|
To fix, make the variable local and pass it around from function to function.
Functions
Warning: Type specifier missing
Functions must have a return type declared. C, by default, treats all functions as returning int
if the return type is not declared. It is, however, a good programming practice to always declare the return type explicitly, even if it is returning int
. CS1010 insists on this, and you will be penalized if you do not declare the return type.
E.g.,
1 2 3 |
|
1 2 3 |
|
Warning: Implicit declaration of function
All functions in C must be declared before they are used. If the function is defined elsewhere, the header file containing the function declaration should be included. Without the function declaration, the compiler will guess the type of the arguments and their return type. An incorrect guess would lead to buggy code and thus should be avoided. E.g.,
1 2 3 4 |
|
1 2 3 4 |
|
To fix, include the appropriate header file.
Error: Undefined reference to a function
This error is usually accompanied by an "implicit declaration of function" warning. During linking, clang
tries to locate the definition of a function. Calling a function that is not defined would lead to the error above. E.g.,
1 2 3 |
|
1 2 3 |
|
Error: Too many/few arguments to a function call
Each function should be called with exactly the number of arguments defined.
E.g.,
1 2 3 4 5 |
|
1 2 3 |
|
To fix, check the documentation or the man
page of the function you are calling to understand the number of arguments needed.
Warning: Control reaches the end of non-void function
Every non-void function, except main
, must return a value. If you define a non-void function but did not include a return
statement, the compiler would warn you. Failing the return the intended value means the caller would not receive the correct value back, leading to a buggy code.
E.g.,
1 2 3 |
|
1 2 |
|
To fix this, double-check if the function needs to return anything. If not, change the return type to void
. Otherwise, return the appropriate value.
Warning: Parameter is not declared, defaulting to type int
The type of each parameter to a function must be declared explicitly. Not doing so would lead to code that is cognitively harder to understand and bug-prone than necessary.
For example,
1 2 3 |
|
1 2 3 |
|
To fix, declare an appropriate type for each parameter.
Warning: Unused Parameter
Every parameter that you pass into a function must serve a purpose and so should be used.
For instance,
1 2 3 |
|
1 2 3 |
|
x
if you do not need it or check that you do not unintentionally leave x
unused.
Logic
Warning: Expression result unused
The result of your expression should be used. Otherwise, the computation is wasted. For instance:
1 2 3 4 |
|
1 2 3 |
|
To fix, check if the expression is necessary. If so, use it as intended. Otherwise, remove it.
Warning: Code/return/break will never be executed
The execution flow of your code is incorrect. Part of the code will never be executed and is redundant.
1 2 3 4 5 |
|
1 2 3 |
|
To fix, check the logic of your code and remove redundant code.
Warning: Comparing floating point with == or != is unsafe
Floating numbers should never be compared with ==
operator since the representation is not precise.
1 2 3 4 |
|
1 2 3 |
|
To fix this, use >
and <
comparison with a small error. For instance,
1 2 |
|