Q:

Taking EOF as input in C .

I want to print the value of EOF in C . 
char ch =getchar() ; 
printf("%d",ch) ; 

this correctly prints the value -1 when I type in CTRL + Z (I am working in windows). 
While.
char ch ;
scanf("%c",&ch) ;
printf("%d",ch) ;

incorrectly prints 126 . 
What is the reason for this unusual behaviour ? 

A:

EOF is intended to be distinguishable from any value that could be contained in the file being read.
As such, when you're reading from a file, and might receive an EOF, you should not use a char to hold that value. You should use an int, and only convert the result to char after you've verified that what you received was not an EOF. To get correct results, try something like this:
int char;
ch = getchar();
printf("%d", ch);

For your second example, when you read data with scanf, you find out how much (if any) data is read by checking its return value. If it encountered the end of a file, it will simply stop trying to convert input, and you'll find out how many fields were successfully converted by checking its return value. It won't try to convert EOF into a variable.

