locked
buffer size of printf() in VC2010 RRS feed

  • Question

  • What's the actual size of the buffer of printf() function in VC2010?

    As we know, printf() will print the string from the buffer in 3 situations, the buffer is full / getting user input / meet '/n' character.

    However, I got a situation which didn't satisfy those 3 situations unless the buffer of printf() is less than 1 char....

    In the following code, the string "H" only occupy the buffer 1 char unit, but still be printed before the for-looping.

    *I'm using ANSI C in programming

    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
    	int n;
    
    	printf ("H");
    
    	for (n = 1; n < INT_MAX; n++)
    	{
    
    	}
    
    	printf ("i");
    	getchar();
    	return 0;
    }



    Sunday, August 11, 2013 10:45 AM

Answers

  • "As we know, printf() will print the string from the buffer in 3 situations, the buffer is full / getting user input / meet '/n' character."

    As I know there are 3 types of stream buffering:

    • unbuffered - there's no buffer, everything is sent to the file as soon as possible. stdout is unbuffered by default in Visual C++
    • line buffering - the text is sent to the output when a new line character is encountered - Visual C++ doesn't support this mode
    • fully buffered - characters are sent to the output when the buffer is full (and when input is requested) - you can use setvbuf to make stdout to fully buffered in Visual C++ but the "input is requested" part is not supported.
    • Marked as answer by richardchung Sunday, August 11, 2013 1:14 PM
    Sunday, August 11, 2013 11:33 AM

All replies

  • "As we know, printf() will print the string from the buffer in 3 situations, the buffer is full / getting user input / meet '/n' character."

    As I know there are 3 types of stream buffering:

    • unbuffered - there's no buffer, everything is sent to the file as soon as possible. stdout is unbuffered by default in Visual C++
    • line buffering - the text is sent to the output when a new line character is encountered - Visual C++ doesn't support this mode
    • fully buffered - characters are sent to the output when the buffer is full (and when input is requested) - you can use setvbuf to make stdout to fully buffered in Visual C++ but the "input is requested" part is not supported.
    • Marked as answer by richardchung Sunday, August 11, 2013 1:14 PM
    Sunday, August 11, 2013 11:33 AM
  • But according to msdn, the default size of a stream buffer is 4K.

    See: msdn.microsoft.com/en-us/library/c565h7xx(v=vs.100).aspx

    Sunday, August 11, 2013 12:15 PM
  • But that's a generic statement about the default buffer size, it doesn't imply in any way that stdout is buffered.

    And according to that very MSDN page stdout and stderr are not buffered:

    "The stdout and stderr functions are flushed whenever they are full or, if you are writing to a character device, after each library call"

    • Proposed as answer by Pavel A Sunday, August 11, 2013 1:28 PM
    Sunday, August 11, 2013 12:31 PM
  • What's the meaning of "writing to a character device, after each library call"? Does it mean waiting the user to enter character?

    The buffer is flushed not due to getchar(),  it's flushed even before the for-looping in my code, so I don't understand.

    Sorry for gabbling.

    Sunday, August 11, 2013 12:52 PM
  • "After each library call" - printf is such a library call, the buffer is flushed after each printf call.
    Sunday, August 11, 2013 1:06 PM