Thursday 15 August 2013

How to pause between functions in a program in C with GCC? -


So my problem is that my program runs very fast so that I do not think how it behaves. It should crawl the text with the edges of the terminal. I tried to make a short break between printf (...) s to use sleep () so that I can see where this text is going is. / P>

This is what it looks like:

So I put the Function () function after printf So that it breaks the loop before starting again and move the text gradually, but what happens is that it prevents programs indefinitely even before the program starts. It also happens with sleep and system ("pause 1") . This is what looks like this:

==================================== = ================================================= ====

EDIT:

OK, I put it on my own. It seems that sleep () works only when I put \ n in my string I do not know why I did not even read it in very manual.

So if you have

  printf ("hello \ n"); Sleep (3); Printf ("hello \ n"); Sleep (3); Printf ("hello \ n");   

The result will be:

Hello

[pause for 3 seconds]

Hello

[Pause for 3 seconds]

Hello

But if You remove the new line

[Break for 9 seconds] Hello Hollow Hollo

I do not know why this happens, but it just does Is

========================================== ===== =================================== EDIT:

In this way I wanted to work for my work Rum:

Thank you for your reply

Your comments are not the reason So () is not working, but by the fact that uses printf () stdout , which is the line buffer "line Buffer "means the buffer's content of buffer that is written on stdout is buffer until the content is reached before the remaining.

Then first printf ("helo"); The output does not go to the screen but remains in buffer, then Sleep (1); is executed and you wait for the next printf ("HELLO"); The output still does not go to the screen, but the following sleep (1); Again you will have 1 second ... and so on until a line end in your program by a \ n or by the end of the program, which flushes the output buffer in the console.

You can output buffer of stdout to each printf () :

  #include & lt; Stdio.h & gt; After flushing can avoid behavior. # Include & lt; Unistd.h & gt; Int main (zero) {printf ("hello"); Fflush (stdout); Sleep (3); Printf ("hello"); Fflush (stdout); Sleep (3); Printf ("hello"); / * Fluff (standout); * / / * Is not required because buffers are flipped at the end of the program / / 0 returns; }    

No comments:

Post a Comment