source code of /small-scripts/stackexchange/spinner.c

Last modified
Lines 35

Parent directory Download CGIread sitemap Main page

Quick links: (none)

  1. /*
  2. 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.
  3. 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.
  4. */
  5.     #ifdef _WIN32
  6.         #include <windows.h>
  7.         #define sleepThr(x) Sleep(x)
  8.     #else
  9.         #include <unistd.h>
  10.         #define sleepThr(x) usleep((x) * 1000)
  11.     #endif
  12.     #include <stdio.h>
  13.     #define INTERVAL 200
  14.     int main()
  15.     {
  16.         int counter;
  17.         while (1)
  18.         {
  19.             for (counter = 0; counter < 4; counter++)
  20.             {
  21.                 printf("\b%c", "|/-\\"[counter]);
  22.                 fflush(stdout);
  23.                 sleepThr(INTERVAL);
  24.             }
  25.         }
  26.         return 0;
  27.     }