The man page say:
The value EOF is returned if the end of input is reached before
either the first successful conversion or a matching failure occurs.
EOF is also returned if a read error occurs, in which case the error
indicator for the stream (see ferror(3)) is set, and errno is set to
indicate the error.
This means that you can check against 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;
}
Or:
#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;
}
This applies for:
scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf - input format con‐
version