{ }codelper

오류 해결 사전 › C언어

C언어 undeclared (first use in this function)

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

작은따옴표 안의 이름을 가진 변수를 이 자리에서는 컴파일러가 모른다는 오류예요. C에서는 변수를 쓰기 전에 int sum;처럼 타입과 함께 선언해야 해요. 오류가 가리키는 줄에서 그 이름을 찾고, 그보다 위에 선언이 있는지, 철자가 같은지, 같은 중괄호 안인지 보면 돼요.

원인 1. 선언을 하지 않았어요

파이썬처럼 sum = 0이라고만 쓰면 C에서는 오류예요. 어떤 타입의 변수인지 먼저 알려 줘야 해요.

오류 아래의 note: each undeclared identifier is reported only once ...는 "같은 이름은 함수마다 한 번만 알려 준다"는 안내예요. sum을 여러 줄에서 썼어도 오류는 처음 쓴 줄 하나만 나와요.

틀린 코드

#include <stdio.h> int main(void) {    sum = 0;    for (int i = 1; i <= 10; i++) {        sum += i;    }    printf("%d\n", sum);    return 0;}
실행하면
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

고쳐서 실행해 보기 →

고친 코드

처음 쓰는 곳에서 타입을 붙여 선언해요.

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

고쳐서 실행해 보기 →

원인 2. 이름을 잘못 적었어요

선언한 이름과 쓰는 이름이 한 글자라도 다르면 다른 변수예요. 대문자와 소문자도 구별해요. 비슷한 이름의 변수가 있으면 GCC가 did you mean 'count'?라고 알려 주니 그 줄을 놓치지 마세요.

틀린 코드

#include <stdio.h> int main(void) {    int count = 0;    for (int i = 0; i < 5; i++) {        conut++;    }    printf("%d\n", count);    return 0;}
실행하면
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

고쳐서 실행해 보기 →

고친 코드

선언한 이름과 똑같이 적어요.

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

고쳐서 실행해 보기 →

원인 3. 중괄호 밖에서 썼어요

변수는 선언한 중괄호 { } 안에서만 살아 있어요. for (int i = 0; ...)의 i는 그 반복문이 끝나면 사라져서, 반복문 밖에서 쓰면 모르는 이름이 돼요. if나 while의 중괄호 안에서 선언한 변수도 마찬가지예요.

틀린 코드

#include <stdio.h> int main(void) {    for (int i = 0; i < 3; i++) {        printf("%d\n", i);    }    printf("end %d\n", i);    return 0;}
실행하면
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

고쳐서 실행해 보기 →

고친 코드

반복문이 끝난 뒤에도 쓸 변수는 반복문보다 앞에서 선언해요.

#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;}
실행 결과
0
1
2
end 3

고쳐서 실행해 보기 →

다른 함수 안에서 선언한 변수도 쓸 수 없어요. main의 변수를 다른 함수에서 쓰려면 인수로 넘겨요.

원인 4. true, false를 썼어요

C17까지의 C에서 true, false, bool은 헤더를 넣어야 쓸 수 있는 이름이에요. 헤더가 없으면 컴파일러는 true를 선언하지 않은 변수로 봐요.

틀린 코드

#include <stdio.h> int main(void) {    int found = true;    if (found) {        printf("yes\n");    }    return 0;}
실행하면
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;
      |                 ^~~~

고쳐서 실행해 보기 →

고친 코드

#include <stdbool.h>를 넣어요. 그러면 bool 타입도 쓸 수 있어요.

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

고쳐서 실행해 보기 →

NULL도 헤더에 들어 있는 이름이에요. 'NULL' undeclared가 나오면 <stdio.h>나 <stdlib.h>를 넣으세요.

이 오류가 났을 때 확인할 것

  1. 작은따옴표 안의 이름과 오류의 줄 번호를 보세요. 그 줄에서 그 이름을 찾아요.
  2. did you mean ...? 줄이 있으면 오타일 가능성이 커요.
  3. 그 줄보다 위에 int 이름; 같은 선언이 있는지 확인하세요.
  4. 선언이 있다면 같은 중괄호 안인지 확인하세요. 닫는 }를 지나면 그 안에서 선언한 변수는 쓸 수 없어요.
  5. 문자열에 큰따옴표를 빠뜨리지 않았는지도 보세요. printf(hello);의 hello는 변수 이름으로 읽혀요.
  6. 컴파일러마다 글이 달라요. clang은 use of undeclared identifier, Visual Studio는 error C2065(undeclared identifier)로 알려 줘요.

연습 문제

더 보기