2

I was just wondering why my code does not register my CTRL+ Z input? When i input CTRL+ Z it does not exit the while loop somehow.

However when I substitute the scanf() function with a getchar() function, it suddenly works! could anyone out there help enlighten me on this?

#include <stdio.h>
#include <stdlib.h>

int main(){
    char grade;
    while( grade != EOF){
        scanf("%c" , &grade);
        switch(grade){
        case 'a':
            printf("you got an a!\n");
            break;
        case 'b':
            printf("you got a b!\n");
            break;
        case'\n':
            break;
        default:
            printf("error :(\n");
            break;
        }
    }
    printf("Hello world!\n");
    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Javier Lam
  • 21
  • 3
  • Related Question : http://stackoverflow.com/questions/17220158/exiting-a-while-loop-at-eof-using-scanf-in-c – msc Dec 08 '16 at 13:30
  • Possible duplicate of [While scanf EOF loop misbehaving](http://stackoverflow.com/questions/27713972/while-scanf-eof-loop-misbehaving) – davmac Dec 08 '16 at 13:44
  • it's worth noting that Ctrl-Z is the EOF character *on Windows*. UNIX / Linux / POSIX systems all use Ctrl-D for this (AFAIK). – David Dec 08 '16 at 13:48
  • Note that `grade` is not initialized when `while( grade != EOF){` first executes. – chux - Reinstate Monica Dec 08 '16 at 16:21
  • to start 1) the value EOF is a signed integer, not a character. 2) ALWAYS check the returned value, not the parameter value when calling `scanf()` because the EOF value will NOT be placed in the parameter. 3) the variable `grade` is not initialized before being checked. 4) strongly suggest the `while()` statement be written as: `while( EOF != scanf( "%c", &grade ) )` Suggest always reading/understanding the man page for any system functions called. – user3629249 Dec 09 '16 at 10:16

1 Answers1

1

You need to check the return value of scanf() function to get the expected EOF. Checking the argument for EOF after the scanning failed is wrong, and may invoke undefined behavior, in case the argument is of type automatic local and not initialized.

Quoting from the man page (emphasis mine)

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 indicate the error.

That said, a char is not enough to hold a value of EOF, if you have to store the return value of scanf() call, use an int.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • oh do you mean I should simply change the grade to an integer variable? Ive tried that but it still doesnt work tho... – Javier Lam Dec 08 '16 at 13:26
  • 2
    @JavierLam nope, check the **return** value of `scanf()` function call. – Sourav Ghosh Dec 08 '16 at 13:27
  • Sorry I know it a really noob question, but how do I check the return value of the scanf() function call?:) If its too cumbersome to explain I will understand! I have to sa my basics of programming is rather shaky as I am still new – Javier Lam Dec 08 '16 at 13:31