source code of /small-scripts/stackexchange/spinner.c
Last modified | |
Lines | 35 |
Parent directory Download CGIread sitemap Main page
Quick links: (none)
/*
You could use counter as an index to a character array instead of a big clunky repetitive switch. If you have a char *spinner = "|/-\\";, spinner[0] will be "|", spinner[1] will be "/", etc.
char *spinner = "|/-\\"; printf("\b%c", spinner[counter]); is going to print the common backspace character and the correct "spinner symbol" for each value of counter. The spinner variable does not need to be declared because it's a string literal: printf("%c", "|/-\\"[counter]); can be used instead.
*/
#ifdef _WIN32
#include <windows.h>
#define sleepThr(x) Sleep(x)
#else
#include <unistd.h>
#define sleepThr(x) usleep((x) * 1000)
#endif
#include <stdio.h>
#define INTERVAL 200
int main()
{
int counter;
while (1)
{
for (counter = 0; counter < 4; counter++)
{
printf("\b%c", "|/-\\"[counter]);
fflush(stdout);
sleepThr(INTERVAL);
}
}
return 0;
}