In the manual about functions of buffer-using given at http://man7.org/linux/man-pages/man3/setbuf.3.html, a piece of code below is declared as invalid. However, when I try that on my machine, things go well:
#include <stdio.h>
int
main(void)
{
char buf[BUFSIZ];
setbuf(stdin, buf);
printf("Hello, world!\n");
return 0;
}
The reason given by that age is :
You must make sure that the space that buf points to still exists by the time stream is closed, which also happens at program termination.
I can't see any mistake in that piece of code. And it also goes well too. Is it right or not? Can any one explain it to me?
################################################2as @Lingxi have answered below, the buffer will be destroyed after the main function return. But stdin
survive.
But, is there a default buffer left for the stdin
?
And also, can we assume that the stdin
is a stream, and there is some ways to destroyed or close that stream?