{ }codelper

Error Messages › C

C undeclared (first use in this function)

error: ‘sum’ undeclared (first use in this function)

This error means the compiler does not know, at this spot, a variable with the name in single quotes. In C you have to declare a variable with its type, as in int sum;, before you use it. Find that name on the line the error points to, then check whether there is a declaration above it, whether the spelling matches, and whether it is inside the same braces.

Cause 1. You didn't declare it

Writing just sum = 0, as you would in Python, is an error in C. You have to say what type the variable is first.

The note: each undeclared identifier is reported only once ... under the error is a notice that the same name is reported only once per function. Even if you used sum on several lines, you get just one error, for the line where it is first used.

Wrong code

#include <stdio.h> int main(void) {    sum = 0;    for (int i = 1; i <= 10; i++) {        sum += i;    }    printf("%d\n", sum);    return 0;}
When you run it
main.c: In function ‘main’:
main.c:4:5: error: ‘sum’ undeclared (first use in this function)
    4 |     sum = 0;
      |     ^~~
main.c:4:5: note: each undeclared identifier is reported only once for each function it appears in

Edit and run it →

Fixed code

Declare it with a type where you first use it.

#include <stdio.h> int main(void) {    int sum = 0;    for (int i = 1; i <= 10; i++) {        sum += i;    }    printf("%d\n", sum);    return 0;}
Output
55

Edit and run it →

Cause 2. You misspelled the name

If the name you use differs from the name you declared by even one letter, it is a different variable. Upper and lower case count too. When there is a variable with a similar name, GCC tells you with did you mean 'count'?, so don't miss that line.

Wrong code

#include <stdio.h> int main(void) {    int count = 0;    for (int i = 0; i < 5; i++) {        conut++;    }    printf("%d\n", count);    return 0;}
When you run it
main.c: In function ‘main’:
main.c:6:9: error: ‘conut’ undeclared (first use in this function); did you mean ‘count’?
    6 |         conut++;
      |         ^~~~~
      |         count
main.c:6:9: note: each undeclared identifier is reported only once for each function it appears in

Edit and run it →

Fixed code

Write the name exactly as you declared it.

#include <stdio.h> int main(void) {    int count = 0;    for (int i = 0; i < 5; i++) {        count++;    }    printf("%d\n", count);    return 0;}
Output
5

Edit and run it →

Cause 3. You used it outside the braces

A variable is alive only inside the braces { } where it was declared. The i in for (int i = 0; ...) disappears when that loop ends, so using it outside the loop makes it an unknown name. The same goes for a variable declared inside the braces of an if or while.

Wrong code

#include <stdio.h> int main(void) {    for (int i = 0; i < 3; i++) {        printf("%d\n", i);    }    printf("end %d\n", i);    return 0;}
When you run it
main.c: In function ‘main’:
main.c:7:24: error: ‘i’ undeclared (first use in this function)
    7 |     printf("end %d\n", i);
      |                        ^
main.c:7:24: note: each undeclared identifier is reported only once for each function it appears in

Edit and run it →

Fixed code

Declare a variable you still need after the loop before the loop.

#include <stdio.h> int main(void) {    int i;    for (i = 0; i < 3; i++) {        printf("%d\n", i);    }    printf("end %d\n", i);    return 0;}
Output
0
1
2
end 3

Edit and run it →

You can't use a variable declared inside another function either. To use a variable from main in another function, pass it as an argument.

Cause 4. You used true or false

In C up to C17, true, false and bool are names you can only use after including a header. Without the header, the compiler sees true as an undeclared variable.

Wrong code

#include <stdio.h> int main(void) {    int found = true;    if (found) {        printf("yes\n");    }    return 0;}
When you run it
main.c: In function ‘main’:
main.c:4:17: error: ‘true’ undeclared (first use in this function)
    4 |     int found = true;
      |                 ^~~~
main.c:2:1: note: ‘true’ is defined in header ‘<stdbool.h>’; did you forget to ‘#include <stdbool.h>’?
    1 | #include <stdio.h>
  +++ |+#include <stdbool.h>
    2 |
main.c:4:17: note: each undeclared identifier is reported only once for each function it appears in
    4 |     int found = true;
      |                 ^~~~

Edit and run it →

Fixed code

Add #include <stdbool.h>. Then you can use the bool type as well.

#include <stdio.h>#include <stdbool.h> int main(void) {    bool found = true;    if (found) {        printf("yes\n");    }    return 0;}
Output
yes

Edit and run it →

NULL is also a name that comes from a header. If you see 'NULL' undeclared, include <stdio.h> or <stdlib.h>.

What to check when you see this error

  1. Look at the name in single quotes and the line number in the error. Find that name on that line.
  2. If there is a did you mean ...? line, it is very likely a typo.
  3. Check that there is a declaration such as int name; above that line.
  4. If there is a declaration, check that it is inside the same braces. Once you pass the closing }, variables declared inside can't be used.
  5. Also check that you didn't leave the double quotes off a string. The hello in printf(hello); is read as a variable name.
  6. The message differs between compilers. clang says use of undeclared identifier, and Visual Studio says error C2065 (undeclared identifier).

Practice problems

See also