To resolve the “Undefined Identifier ‘sleep’” error in your C program, follow these steps:
-
Include the Time Header: Add #include <time.h> at the top of your source file. This header declares the sleep function.
-
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 2 3 4 5 6 7 8 9 |
#define _POSIX_C_SOURCE #include <time.h> int main() { sleep(5); // Pauses for 5 seconds return 0; } |
- 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.