Nature of printf?

Hello guys, im new in the forum and in C ... I have a small doubt ...

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <time.h>

int main(void)
{
    int x;
    while (1)
        if (x<time(NULL))
        {
            x=time(NULL);
            printf("%.2d", x%60);
        }
}


During the count, the printf doesn't print, only if I add the "\n" ... why?
Also noticed in the following...

1
2
3
printf("lol1");
printf("lol2");
printf("lol3");


it works perfectly ... something about the call stack? thank you so much ;)
printf is buffered.

What you send to it is not output immediately, but is stored in an internal array. That array is not output to the screen until the buffer is "flushed".

Once the printf implementation decides it has enough data (which is usually after several calls, or one large call, or on a newline), it will automatically flush.

If you want to manually flush, there's a fflush() function:

1
2
printf("%.2d", x%60); // <- may or may not actually output to the screen
fflush(stdout);  // <- now it definitely will be output to the screen 
1
2
 // or we can turn off buffering for stdout with 
setvbuf( stdout, NULL, _IONBF, 0 ) ;
Many thanks for the replies, guys, cleared my mind ... : D
Topic archived. No new replies allowed.