setbuf C Library Function

The function void setbuf(FILE *stream, char *buffer); sets the buffer to be used by a stream for Input/Output operations. If buffer argument is NULL, buffering is disabled for that stream. The setbuf function must be called once the file associated with the stream has already been opened, and before doing any input or output operation on stream.

Function prototype of setbuf

void setbuf(FILE *stream, char *buffer);
  • stream : A pointer to a FILE object which identifies a stream.
  • buffer : This is pointer to a memory block to be used as buffer for given stream. The size of this buffer must be at least BUFSIZ bytes.

Return value of setbuf

NONE

C program using setbuf function

The following program shows the use of setbuf function to set buffer for stdout stream.

setbuf C Library Function

#include <stdio.h>
 
int main(){
   char buffer[BUFSIZ];
   /*Setting buffer for stdout stream */
   setbuf(stdout, buffer);
   puts("setbuf C Standard library function");
   /* Flushing the buffer, 
      Now string got printed on screen */
   fflush(stdout);
    
   getch();
   return(0);
}

Output

setbuf C Standard library function