print buffer refresh problem

printf buffer problem

The following content may be different when testing in Linux and Windows.

1. Introduction

There is a buffer for the printf output function, which was discovered when using the sleep function test.
First, let’s recap the test question:
simply write a hello world program

#include <stdio.h>

int main()
{
    
    
    printf("hello world\n");
    sleep(5);  //延迟5秒
     printf("hello friend\n");
    return 0;
}

Output result: There is an interval of 5 seconds between
insert image description here
the output of hello world and hello friend

When we modify the code: delete the \n line break after hello world

#include <stdio.h>

int main()
{
    
    
    printf("hello world");
    sleep(5);  //延迟5秒
     printf("hello friend\n");
    return 0;
}

Output:
insert image description here
This output is:The cursor blinks for 5s and then pops up hello worldhello friend

Here we will find that: when we delete the character '\n', the function sleep is no longer a delay between statements, but a delay of the entire program.

The results here are very surprising. I have never noticed or thought about this problem. Let’s understand printf in depth.

2. In-depth understanding of printf

printf is a line buffer function, it does not directly output the data to the screen, but first puts it in the buffer, and only after certain conditions are met, the contents of the buffer will be output.

Setting the buffer is to improve the IO speed and reduce the waste of CPU resources caused by the CPU waiting for IO.

The following 5 conditions can refresh the buffer:

1. The buffer is full
2. There are '\n' and '\r' in the written characters
3. Call fflush to manually refresh the buffer
4. When calling scanf to read data from the buffer, the buffer will also be Data Refresh within
5. At the end of the program

1. The buffer is full

The buffer size of the printf function is 1024 bytes. When the size of the buffer is exceeded, the buffer will be refreshed and the result will be printed.

The buffer size is 1024 bytes, this size is obtained in this way, the code is as follows:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 /*argc:命令行输入参数个数,argv:命令行参数 
 5  *argv为字符指针数组,argv[i]为指向第i个命令行参数内容的指针
 6  */
 7 int main(int argc, char **argv){
    
     
 8     int i;
 9     char a='a';
10     if(argc != 2) //命令行参数为2,否则出错
11     {
    
    
12         printf("Usage:%s Number\n",argv[0]); 
13         return 0;
14     }
15 
16     for(i=0;i<atoi(argv[1]);i++) //atoi:字符转化为整数
17     {
    
    
18         printf("%c",a);
19     }
20     
21     while(1);  //让程序一直运行
22 }

Running result:
insert image description here
Description : Under Linux, the printf buffer size is 1024 bytes. while(1) keeps the program running, when the buffer is not full, no output will be printed.

2. There are '\n' and '\r' in the written characters.

Test code:

#include <stdio.h>

int main()
{
    
    
    printf("hello world\n");//
    sleep(5);  //延迟5秒
     printf("hello friend\n");
    return 0;
}

Running result:
insert image description here
3. Call fflush to manually refresh the buffer

Test code:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(void)
{
    
    
    printf("hello world");
    fflush(stdout);
    sleep(5);
    exit(0);
}

Running process and results:
insert image description here
insert image description here
Here, after the printf statement ends, use fflush to force the buffer to be flushed, first print out the content, and then execute the sleep statement.

4. When calling scanf to read data from the buffer, the data in the buffer will also be refreshed

We can understand this as when we enter from the keyboard, the data in the data will be automatically refreshed.

5. At the end of the program

Test code:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(void)
{
    
    
    printf("hello world");
    sleep(5);
    exit(0);
}

operation result:
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_56935264/article/details/124760432