For parentheses in c

I am writing a program to print integer values, using for a loop and after printing, the program has to wait one second, after which these integers are overwritten with a double space character, in other words, the purpose of the program is to erase these integers after waiting for one seconds.

This program:

#include <stdio.h>
#include <time.h>

int main (void) {
   int i;

   for(i=1;i<=5;i++){
       printf("%d ",i);
   }

   for(;clock () < CLOCKS_PER_SEC;){} /*wait for one second*/

   printf("\r");     /*move to the beginning of the line*/ 
   for(i=1;i<=5;i++){
      printf("  ");   /*overwriting the integers*/
   }
   printf("\n");

   return 0;
}

      

The problem is in the wait loop brackets for: (; clock () <CLOCKS_PER_SEC;) {} 'when i remove those brackets for the program to work correctly. but if a for loop with brackets. the program doesn't work, I mean the program is still running, but it overwrites the integer instead of showing those integers.

please someone explain what will happen?

+3


source to share


5 answers


You don't flush stdout (the stream that printf prints), so it doesn't happen until '\ r' is written, and then you flush it right away.

If you remove {}

then your loop is equivalent to

for(;clock () < CLOCKS_PER_SEC;)
    printf("\r");

      



which writes a beam \r

, the first one clears the output, and the rest of them are redundant. At the end of the loop, you clear the line, working the way you want.

After printing the numbers, you must call fflush(stdout)

. Or you can move printf ("\ r") so that it goes to the wait loop (the difference is where the cursor is).

Your loop is problematic as there is no guarantee that clock () will start at 0 and it won't be used on many systems and you shouldn't spin like that ... it slows down other programs running on your system. You can just use sleep (1), although it's not very accurate.

+2


source


When you remove the parentheses, the printf ("\ r") statement becomes the body of the for loop, logically equivalent to this:

for(;clock () < CLOCKS_PER_SEC;) {printf("\r");}

      



Thus, integers will be overwritten immediately after the end of the delay period.

Of course, the real question is why are you using a busy loop to delay and not just call sleep (1), which is much more efficient (i.e. it won't bind your cpu to 100% during the delay period)

+2


source


I suspect that the output buffer gets blurry differently between the two. You can check this by manually flushing the buffer using fflush(stdout)

before the problem loop.

Also note that {}

it is optional in C for one-line statements inside a loop.

+1


source


In the first "for" loop, you print the values ​​using printf. Here 'printf' uses 'stdout' which is the buffered output of the output value, will not be printed unless "\ n" is specified or the buffer is full. so you can use flush(stdout)

after the first loop, or use fprintf(stderr, "")

to print standard error, which is not buffered output.

0


source


Here's the code you might want:

#include <stdio.h>

int main(int argc, char * argv[]) {
    int seconds = 10;

    while(seconds>0) {
        printf("%10d", --seconds);
        fflush(stdout);
        sleep(1);
        printf("\r");
    }
    printf("%10s\n", "time up!");

    return 0;
}

      


(Since you are asking about fflush () being actural, here is a little explanatory base for my understanding)

All about io cache , 1 reason cache exists: read / write memory can be over 1000 times faster than a hard drive.

So the program should try to reduce the read / write frequency of the hard drive and use memory instead, but this requires a proper compromise for user and io latency.

eg

  • When you read a file line by line, it can read 2KB at once, rather than one line, and then read from the memory cache.
  • When writing to the console, the program may choose to write to the memory cache until it encounters \ n or \ t char, and in another case.

fflush (file FILE *) , is a function from stdio.h, it clears the cache of the specified FILE. In your case, the file is stdout which prints to the console. When you use printf () to print a single number, it can write the stdout file to the cache, so you didn't see it in the console, but calling fflush (stdout) flushes the cache to the console.


0


source







All Articles