IT Log

Record various IT issues and difficulties.

How to Fix Undefined Identifier ‘sleep’


To resolve the “Undefined Identifier ‘sleep’” error in your C program, follow these steps:

  1. Include the Time Header: Add #include <time.h> at the top of your source file. This header declares the sleep function.

  2. Define POSIX Compliance: To ensure compatibility with the POSIX standard (which includes the sleep function), define _POSIX_C_SOURCE before including <time.h>. You can do this by adding #define _POSIX_C_SOURCE either before or after the include statement.

Here’s an example of how your code should look:

  1. Recompile Your Program: After making these changes, recompile your program to ensure the error is resolved.

By including <time.h> and defining _POSIX_C_SOURCE, you inform the compiler about the POSIX functions, allowing it to recognize sleep.


, , , ,