C: undeclared (first use in this function)
Causes and Solutions for This Error
When attempting to compile the C source file `test.c` using the `gcc` command on Linux (CentOS 6.4), the following error occurred.
undeclared (first use in this function)
Command executed when the C compilation error occurred
[root@test test]# gcc test.c
test.c: In function 'main':
test.c:4: error: 'i' undeclared (first use in this function)
test.c:4: error: (Each undeclared identifier is reported only once
test.c:4: error: for each function it appears in.)
Contents of the source file test.c
To investigate the cause of the C language compilation error, check the source file test.c.
#include
int main(void){
for(i=0;i<10;i++){
printf("*");
}
}
Compare the content of the compilation error message with the contents of the source file.
A direct translation of the error message "test.c:4: error: 'i' undeclared (first use in this function)" yields the following meaning.
Error on line 4 of test.c: "i" is undeclared (first use in this function)
If you want to use "i" as a variable, you must declare it. It appears the compilation error occurred because the declaration of the variable "i" was omitted.
To resolve this C language compilation error, modify the source file test.c as follows.
Contents of the modified source file test.c
#include
int main(void){
int i;
for(i=0;i<10;i++){
printf("*");
}
}
I added the line "int i;" to declare the variable i as type int immediately before the for loop.
With this source file, compilation using the gcc command was successful, and the C language compilation error that was the issue this time has been successfully resolved.