How come fflush(stdin) function is not working?

前端 未结 2 385
逝去的感伤
逝去的感伤 2021-01-26 05:17

My main question is why is it that the fflush(stdin); function not working? Whenever I run the the code, I am not able to get the second input with space ex. Hello World but in

相关标签:
2条回答
  • 2021-01-26 05:41

    If you get any output at all, it would be because the code you have displayed in the problem description area, is not the code you are actually using.

    Regarding your statement:
    I am not able to get the second input with space ex. Hello World but instead I get Hello??.
    Without the additional parameter in the printf() statement, you will get no output, and a runtime error.

    The line (both places) printf("The word you entered was >>%s<<\n"); needs another parameter, add ,string, like this:

     printf("The text you entered was >>%s<<\n", string);
    

    That will fix your problem.

    Here is output after adding the parameter string in printf() (and not removing fflush())
    enter image description here
    Apparently, fflush(stdin); is not really the issue here, at least for stated problem?

    0 讨论(0)
  • 2021-01-26 05:44

    Because fflush(stdin) is undefined behavior. fflush() is only defined by the C standard for output streams, and update streams where the last operation was an output.

    0 讨论(0)
提交回复
热议问题