scanf
이 입력의 수를 반환이 및 할당 읽기 값이 아닌, 그 자체. 이 특별한 경우에는 하나의 입력만을 기대하기 때문에 scanf
은 성공하면 1을, 일치하지 않으면 0을 반환합니다 (즉, 입력이 십진수로 시작하지 않음). 또는 EOF로 끝 점이 있으면 또는 오류. 당신이 입력 값에 대해 테스트하려면
, 당신은
while(scanf(“%d”, &n) == 1 && n == EXPECTED_VALUE)
{
printf(“%d”, n);
}
같은 편집을 할 줄
사실, 즉이 같은 것 할 수있는 더 좋은 방법 :
int n;
int itemsRead;
/**
* Read from standard input until we see an end-of-file
* indication.
*/
while((itemsRead = scanf("%d", &n)) != EOF)
{
/**
* If itemsRead is 0, that means we had a matching failure;
* the first non-whitespace character in the input stream was
* not a decimal digit character. scanf() doesn't remove non-
* matching characters from the input stream, so we use getchar()
* to read and discard characters until we see the next whitespace
* character.
*/
if (itemsRead == 0)
{
printf("Bad input - clearing out bad characters...\n");
while (!isspace(getchar()))
// empty loop
;
}
else if (n == EXPECTED_VALUE)
{
printf("%d\n", n);
}
}
if (feof(stdin))
{
printf("Saw EOF on standard input\n");
}
else
{
printf("Error while reading from standard input\n");
}
'scanf''EOF'이 돌아갈 수 있도록하는 부가하여 '경우 (N! = 1)'체크하여이'while' 조건 '이어야하지만 (는 scanf ("%의 D", N) == 1)'. – user694733