C언어(2020년)
6. scanf_s() 함수
리더2333
2020. 11. 2. 10:22
반응형
scanf_s() 함수는 키보드로 부터 내용을 입렵받는 함수이다.
다음의 예시를 보자
#include <stdio.h>
int main()
{
printf("점수를 입력하세요 : ");
int score = 0;
scanf_s("%d", &score);
printf("score : %d", score);
return 0;
}
실행 후 90 을 입력하고 Enter를 쳐보자
float 는 어떻게 될까
다음과 같이 하면 된다.
#include <stdio.h>
int main()
{
printf("점수를 입력하세요 : ");
float score = 0.0f;
scanf_s("%f", &score);
printf("score : %f", score);
return 0;
}
실행 후 90.1 을 입력하고 Enter 를 쳐보자
90.1 을 입력 했는데, 90.099998 로 나오는 건 어쩔 수 가 없다.
float 는 모든 실수를 표현 할 수 없기 때문에 근사값으로 처리 되기 때문이다.
반응형