Ad space (banner)
๐Ÿ”งC Lessons
Lesson 56 / 68

Waiting for a Fixed Amount of Time (sleep)

This lesson covers pausing execution for a fixed amount of time in C using sleep, so you understand the basics of working with time in a program. It's for anyone searching "C sleep usage."

sleep(seconds) pauses execution for the given amount of time. It's a function provided by the unistd.h header, used to space out API requests at a fixed interval, or to intentionally slow down execution.

The example prints "Starting" then waits 1 second with sleep(1) before printing "Ran after 1 second." Running it, you can feel the 1-second gap between the two messages.

A common early mistake is not knowing which header provides sleep() depends on the OS. unistd.h is for Unix-like systems, and Windows needs a different function (like Sleep()) instead.

In real projects, sleep is used in scenarios like an embedded device's program, where you want to repeat logic at a fixed interval.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)