Is there are way to tell if standard input is empty without using getchar()?

Viewed 45

In the FIRST code below "After" will not print until the user has entered a character and pressed enter because that standard input is empty. In the SECOND code the standard input is not empty and "Before" and "After" will both print immediately without waiting for the user. Is there a way to tell if the standard input is empty so as to avoid the pause if the standard input is empty?

First Code

int main() {
    
    printf("Before");
    char c = getchar();
    printf("After");
}

Second Code

int main() {
    ungetc( 'a', stdin );
    printf("Before");
    char c = getchar();
    printf("After");
}
0 Answers
Related