1

What puts() does in C?

What does it do in the buffer, and what happens to its arguments?

For example, when puts() is passed a string, does puts() send all the strings to the buffer? If a value in a string is set to a null character, whether the character following the null character is sent to the buffer.

Whether the remaining characters in the buffer are still there after printing.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
yida wu
  • 19
  • 2
  • 1
    Very likely it just passes on the parameters to some OS-specific API function. You'll have to dig inside that function to tell what it does, which is only possible for open source OS. Start by checking out glibc on github and take it from there. – Lundin Dec 15 '21 at 12:46
  • 1
    You can imagine that `puts` loops over the pointed-to characters, until it finds the terminating `\0` character (that is, just as for any string-processing function in C), and then, for each character, calls `putchar` on it. Then `putchar` does what `putchar` does, which is generally to stash characters in its own buffer (along with characters from any other output calls you made before or after the call to `puts`), until it has enough to write them all to the outut device, using some OS_dependent lower-level output function — for example, under Unix/Linux, it'd be `write`. – Steve Summit Dec 15 '21 at 12:51
  • No, nothing following the null character is processed at all. And `puts` does not modify the string you hand it, so all the characters in it are still there after printing. – Steve Summit Dec 15 '21 at 12:55
  • On Windows, at the most bottom level in User Mode, there is `WriteFile` function. I've tested it with a user mode hook some time ago. – jtxkopt Dec 15 '21 at 12:57
  • One minor detail that I always seem to forget: `puts` also adds a newline. `fputs` does not, for some absurd historical reason! – William Pursell Dec 15 '21 at 13:28
  • Thank you very much for your answers. I cannot reply to you now. Looks at understanding. – yida wu Dec 15 '21 at 13:40

0 Answers0