Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

c - Example of printf not flushing until newline

It seems that printf is only supposed to flush after a newline (or some other criteria). What would be a way to view this in code? For example, I tried the following test, but it seems to flush after each statement (the getchar I am guessing causes it to flush):

int main(void)
{

    printf("Hi");
    getchar();
    printf("OK!
");

    return 0;
}

Output:

111 PrintF |
           ^
          cursor is here on first `printf`
question from:https://stackoverflow.com/questions/65909391/example-of-printf-not-flushing-until-newline

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
void delay(unsigned someinteger) 
{ 
    clock_t start_time = clock(); 
  
    while (clock() < start_time + someinteger); 
} 

int main(void) 
{ 
        printf("start
");
        printf("Hello");  delay(20000);
        printf("
");
} 

See the delay between start and Hello. (some patience at the beginning required as I had to click run button)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...