0

Does the scanf function automatically add the NULL character at the end of the user's input? And if so, does it also "consume" the character of new line or tab as you click on enter or space? I was wondering about that because as far as I know, gets reads the input, until the user presses enter and then adds the NULL character and throws away the character of new line caused by enter. So what led me to ask this question were programs that include something like this:

This is not a complete program.

scanf("%s", array1);
gets(array2);

Now in this case, gets will read the character of new line and stop there, terminating the program without the user actually giving any input. So basically what I am asking, is what's the difference between scanf and gets as for the details mentioned above.

Note: I've recently learnt that using gets should be completely avoided but since my book and my teachers use it constantly, I'll have to stick to it for now.

Johnny
  • 25
  • 5
  • 1
    Yes it does, not doing so would be pretty pointless. – Jabberwocky Jan 23 '20 at 18:08
  • 5
    Don't use `gets` or `scanf("%s"`. They're security holes. – Petr Skocik Jan 23 '20 at 18:08
  • 1
    Does this answer your question? [What's the difference between gets and scanf?](https://stackoverflow.com/questions/26602608/whats-the-difference-between-gets-and-scanf) – Emiliano Ruiz Jan 23 '20 at 18:13
  • 1
    The correct spelling is the "_null character_". `NULL` is a macro which expands to a _null pointer constant_, which is completely irrelevant to the _null character_. – Lxer Lx Jan 23 '20 at 18:40
  • Related https://stackoverflow.com/questions/12893774/what-is-gets-equivalent-in-c11. – Neil Jan 23 '20 at 21:31

0 Answers0