/errors/c/use-of-undeclared-identifier
C: error: use of undeclared identifier 'count' (gcc: 'count' undeclared)
- tool
- C
- exit code
- 1
- versions
- Clang (all versions), GCC (all versions), C99 and later
- os
- any
error: use of undeclared identifier 'count' (Clang) and error: 'count' undeclared (first use in this function) (GCC) both mean the compiler reached a name that has no declaration visible at that point. In C every variable must be declared before it is used, and a declaration only lives until the end of the block ({ ... }) that contains it. Fix the spelling, declare the variable before its first use in a scope that covers the use, or include the header that defines the constant (EXIT_SUCCESS needs <stdlib.h>).
Reproduce it
Clang 20, from the ziglang package on PyPI (Zig ships a complete Clang), run through uv:
cat > app.c <<'EOF'
#include <stdio.h>
int main(void) {
int counter = 0;
for (int i = 0; i < 3; i++) {
int last = i;
counter++;
}
printf("%d %d\n", count, last);
return EXIT_SUCCESS;
}
EOF
uv run -q --no-project --python 3.13 --with ziglang==0.15.1 python -m ziglang cc -fno-color-diagnostics -c app.c -o app.oOutput · exit 1
app.c:9:23: error: use of undeclared identifier 'count'
9 | printf("%d %d\n", count, last);
| ^
app.c:9:30: error: use of undeclared identifier 'last'
9 | printf("%d %d\n", count, last);
| ^
app.c:10:12: error: use of undeclared identifier 'EXIT_SUCCESS'
10 | return EXIT_SUCCESS;
| ^
3 errors generated.Three different causes in one function: count is a typo of counter, last only exists inside the for block, and EXIT_SUCCESS is a macro from <stdlib.h>, which is not included. GCC reports the same lines as 'count' undeclared (first use in this function), followed by a note that each undeclared identifier is reported only once for each function.
Fix it
cat > app.c <<'EOF'
#include <stdio.h>
#include <stdlib.h> /* EXIT_SUCCESS, NULL */
int main(void) {
int counter = 0;
int last = -1; /* declared outside the loop */
for (int i = 0; i < 3; i++) {
last = i;
counter++;
}
printf("%d %d\n", counter, last);
return EXIT_SUCCESS;
}
EOF
uv run -q --no-project --python 3.13 --with ziglang==0.15.1 python -m ziglang cc -fno-color-diagnostics app.c -o app && ./appOutput
3 2
Common identifiers and their headers
| Identifier | Header |
|---|---|
| NULL, EXIT_SUCCESS, EXIT_FAILURE, size_t | <stdlib.h> (NULL and size_t also in <stddef.h>) |
| errno, ENOENT, EINVAL | <errno.h> |
| INT_MAX, INT_MIN, CHAR_BIT | <limits.h> |
| bool, true, false (before C23) | <stdbool.h> |
| uint8_t, int64_t, UINT32_MAX | <stdint.h> |
| EOF, stdin, stdout, FILE | <stdio.h> |
Globals from another file
A global defined in config.c is not visible in main.c until it is declared there. Put an extern declaration in a header and include it in both files:
extern int verbose; /* declaration: no storage */#include "config.h"
int verbose = 0; /* definition: exactly one .c file */