I've recently wanted to make a "Loading..." display in C where the dots print one at a time in order and then reset:
Suprisingly, there isn't much on the internet for doing this well, so I figured I would make a simple program for it.
#include <stdio.h>
#include <time.h>
int main(void)
{
int msec = 0;
const int trigger = 500; // ms
const int printWidth = 4;
int counter = 0;
clock_t before = clock();
while (1)
{
fputs("Loading", stdout);
clock_t difference = clock() - before;
msec = difference * 1000 / CLOCKS_PER_SEC;
if (msec >= trigger)
{
counter++;
msec = 0;
before = clock();
}
for (int i = 0; i < counter; ++i)
{
fputc('.', stdout);
}
for (int i = 0; i < printWidth - counter; ++i)
{
fputc(' ', stdout);
}
fputc('\r', stdout);
fflush(stdout);
if (counter == printWidth)
{
counter = 0;
}
}
}
The output given is seen in the .gif
above.
I know this can be done better. Any suggestions for improvement?