マニュアルページには次のように書かれています:
最初の変換の成功または一致の失敗が発生する前に入力の終わりに達した場合、値 EOF が返されます。読み取りエラーが発生した場合にも EOF が返されます。この場合、ストリームのエラー インジケータ (ferror(3) を参照) が設定され、エラーを示すために errno が設定されます。
これは、EOF に対してチェックできることを意味します。
#include<stdio.h>
int main(void){
int a;
printf("Please give the value of A: ");
if(scanf("%d",&a) != EOF){
printf("\nThe value of A is\t%d\n",a);
}
return 0;
}
または:
#include<stdio.h>
#include <errno.h>
#include<string.h>
int main(void){
int a, errnum = errno;
printf("Please give the value of A: ");
if(scanf("%d",&a) == EOF){
fprintf(stderr, "Value of errno: %d\n", errno);
perror("Error printed by perror");
fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
}
printf("\nThe value of A is\t%d\n",a);
return 0;
}
これは次の場合に適用されます。
scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf - 入力形式の変換