How do you read scanf until EOF in C?

前端 未结 7 2111
暗喜
暗喜 2020-12-03 02:56

I have this but once it reaches the supposed EOF it just repeats the loop and scanf again.

int main(void)
{
        char words[16];

        while(scanf(\"%1         


        
7条回答
  •  温柔的废话
    2020-12-03 03:20

    You need to check the return value against EOF, not against 1.

    Note that in your example, you also used two different variable names, words and word, only declared words, and didn't declare its length, which should be 16 to fit the 15 characters read in plus a NUL character.

提交回复
热议问题