0

Every stream has an "end-of-file (EOF) flag". The flag is only cleared if you call the clearerr function on the stream. feof(p) function returns the current state of this EOF flag.

https://ibb.co/zGNcCZp

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    int i=0;
    int arr[100];
    while(1){
        scanf("%d",&arr[i]);
        if(feof(stdin)){
            break;
        }
        i++;
    }
    int j;
    for(j=0;j<i;j++){
        printf("%d ",arr[j]);
    }
    i=0;
    printf("\n");
    while(1){
        scanf("%d",&arr[i]);
        if(feof(stdin)){
            break;
        }
        i++;
    }
    for(j=0;j<i;j++){
        printf("%d ",arr[j]);
    }
}
Test Raw
  • 3
  • 2
  • Images of code or error messages are problematic for a number of reasons. Please review https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors and [edit] your question to replace the screen shot with text. – tripleee Feb 04 '21 at 07:18

1 Answers1

0

Why doesn't this code need clearerr for feof?

You are using scanf.

You could read Modern C, then a C draft standard e.g. n1570 or better and look into this C reference website.

That scanf function returns an interesting item count.

On failure, it also sets the feof flag.

You don't need any initial clearerr because the implicit fopen - or the implicit initialization - of stdin (done in crt0 before calling main; see also fdopen) is either calling it or doing the equivalent.

If you use a GNU/Linux system, all of crt0, GCC, the Linux kernel, and GNU libc are open source or free software, and you are allowed to download, study and even improve their source code.

See of course Linux From Scratch. It documents how to compile all that from source code. Also read this related answer and consider installing Debian on your laptop.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • the implicit fopen - or the implicit initialization - of stdin is either calling it or doing the equivalent. What do you mean? – Test Raw Jan 18 '21 at 03:00