0

I want to use miliseconds for my program in c, but there was an error. Basicly im trying to do a halfsecond sleep between between the command.

My code :

#include <stdio.h>
#include <cs50.h>
#include <unistd.h>

int msleep(unsigned int tms) 
{
  return usleep(tms * 1000);
}

int main(void)
{
    int i = get_int("Choose a number.\n");
    for (int a = i; a >= 1; a--)
    {
        printf("%d\n", a);
        sleep(500); 
    }
  {
       printf("blast off!\n");
   } 
    }

My error :

clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    Coding.c  -lcrypt -lcs50 -lm -o Coding
Coding.c:7:10: error: implicit declaration of function 'usleep' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
  return usleep(tms * 1000);
         ^
1 error generated.
<builtin>: recipe for target 'Coding' failed
make: *** [Coding] Error 1

Thank you

  • 1
    `usleep` is not a standard C function. It used to be a POSIX extension, deprecated in 2001 and removed in 2008 (though it may still be available with certain platforms/libraries). See [What Can I Use Besides usleep in a Modern POSIX Environment?](https://stackoverflow.com/questions/7546252/what-can-i-use-besides-usleep-in-a-modern-posix-environment) for alternatives. – dxiv Aug 31 '20 at 17:54
  • Don't use `-std=cXX` until you have a lot more experience; it enables features you actively don't want (like trigraphs), it can expose bugs in the system headers, and what is most directly relevant to you, it tells the C library to minimize the number of extensions it exposes. I strongly suspect that the `usleep` prototype will become available and the program will compile with no complaints if you use `-std=gnu11` instead. – zwol Aug 31 '20 at 18:05
  • (To get accurate diagnostics about violations of the C standard _in your own code_, the options you want are `-std=gnu11 -Wall -Wpedantic`. Add `-D_XOPEN_SOURCE=700` if you do want to minimize your access to C library extensions, without the other problems caused by the hyperconformant `-std=cXX` modes ... but then you'll need to find an alternative to `usleep` again.) – zwol Aug 31 '20 at 18:09
  • (If your instructor told you to use `-std=c11`, that was a mistake on their part, and you may tell them I said so. My email address is discoverable from my profile if they would like to argue with me about it.) – zwol Aug 31 '20 at 18:11

0 Answers0