당신이 부동 입력을 수락 %lf
형식 문자로 scanf
(및 printf
)를 사용하려는 경우,하지 %d
(하는 정수입니다).
scanf 호출이 float 변수에 정수를 쓰고 있으므로 현재 프로그램의 동작은 정의되지 않습니다. 또한 프로그램 상단에 include <stdio.h>
이 누락되었습니다. 이와 같은 오류를 잡으려면 C 컴파일러의 경고를 켜십시오.
$ gcc so-scanf.c -Wall
so-scanf.c: In function ‘main’:
so-scanf.c:6:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
so-scanf.c:6:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
so-scanf.c:7:5: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
so-scanf.c:7:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
so-scanf.c:7:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Wformat]
so-scanf.c:9:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Wformat]
so-scanf.c:11:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Wformat]
so-scanf.c:13:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]
질문에 답변했지만 다른 오류가 있습니다. 'stdlib.h '를 포함하고 있지만'printf'와'scanf'는'stdlib.h'에 정의되어 있지 않습니다. 'stdio.h '가 필요합니다. 그건 그렇고, 컴파일러는 정말로 이러한 모든 문제에 대한 경고를 발행해야합니다. 출력물을 읽었습니까? –