0

I would like to use a delay or sleep function in a C program under Windows64 to create a time delay of about 100ms. Which function is recommended? With the following example i always get the warning: implicit declaration of function usleep.

#include <stdio.h>

int main(void)
{
    printf("Processing... ");

    for(int i=0; i<50; ++i) {

        usleep(100000); // 100ms

        int position = i%4;

        switch(position) {
            case 0: printf("\b|"); break;
            case 1: printf("\b/"); break;
            case 2: printf("\b-"); break;
            case 3: printf("\b\\"); break;
        }
    }
    printf("\nDone!");
    return 0;
}
jxh
  • 69,070
  • 8
  • 110
  • 193
Heimo
  • 39
  • 2

1 Answers1

0

Recommended? That is asking for an opinion.

The following give me satisfactory results (Windows specific, as requested.):

#include <stdio.h>
#include <windows.h>

int main( void ) {
    for( int i = 0; i < 50; i++ ) {
        putchar( "|/-\\"[ i & 3 ] );
        putchar( '\b');
        Sleep( 100 ); // 100ms // NB: Uppercase function name!
    }

    return 0;
}

It's even comical to comment out the second putchar(). (Like an acrobat!)

Fe2O3
  • 6,077
  • 2
  • 4
  • 20