- Home
- Random
- Browse Articles
- Learn Something New
- QuizzesHot
- Forums
- Courses
- Happiness Hub
- Play Games
- This Or That Game
- Train Your Brain
- Explore More
- Support wikiHow
- About wikiHow
- Log in / Sign up
Terms of Use
wikiHow is where trusted research and expert knowledge come together. Learn why people trust wikiHow
',$(a).appendTo("#quiz_small_affiliate_placeholder"),$(a).insertBefore(".youmightalsolike"),a='
',$("#quiz_top_wrap").append(a),$('
').insertBefore("#newsletter_block_main"),G=new IntersectionObserver(function(a,b){a.forEach(function(a){Ja(a.target,a.isIntersecting)})},{rootMargin:"0px 0px 0px 0px"}),Ia(!0),b=document.getElementsByClassName("scrolltomarker"),a=0;a
- Categories
- Computers and Electronics
- Software
- Programming
- C Programming Languages
Download Article
Explore this Article
methods
1The "for-loop" technique
2The "sleep()" Technique
Other Sections
Tips and Warnings
Related Articles
Author Info
Last Updated: August 25, 2023
Download Article
Did you ever want to make a C program wait for a certain time?
You can set up a technique to allow time to tick away, for example: when showing a splash page (a notice or hint) for a game.
Okay, here are some ways to make the program "stand still", read on...
Steps
Download Article
Method 1
Method 1 of 2:
The "for-loop" technique
Download Article
Method 2
Method 2 of 2:
The "sleep()" Technique
Download Article
1
Use sleep() The function called sleep(int ms) declared in <TIME.H>which makes the program wait for the time in milliseconds specified.
2
Include the following line in your program before "int main()":
- #include <TIME.H>
3
Insert, wherever you need your program to make a delay:
- sleep(1000);
- Change the "1000" to the number of milliseconds you want to wait (for example, if you want to make a 2 second delay, replace it with "2000".
- Tip: On some systems the value might refer to seconds, instead of milliseconds. So sometimes 1000 isn't one second, but, in fact, 1000 seconds.
Advertisement
Community Q&A
Search
Question
On my computer, the sleep function works with seconds and I think it accepts integers. How can I drop a delay for half a second?
Community Answer
In the C language, sleep() accepts integers that represent the number of milliseconds the program should wait, which means you need to call sleep(500) to wait half a second.
Thanks! We're glad this was helpful.
Thank you for your feedback.
If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission.Support wikiHowYesNo
Not Helpful 80Helpful 8
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Advertisement
Sample Code
A program that waits a given amount of seconds:
#include <stdio.h>#include <dos.h>int main(){ int del; // The delay period printf("Enter the delay time (in seconds): "); scanf("%i",&del); del *= 1000; // Multiply it by 1000 to convert to milliseconds delay(del); // delay. printf("Done."); return 0;}
A program that counts down from 10 to 0:
#include <stdio.h>#include <time.h>int main(){ int i; for(i = 10; i >= 0; i--) { printf("%i\n",i); // Write the current 'countdown' number delay(1000); // Wait a second } return 0;}
Tips
The above logic can be implemented by using any looping structure followed by a null statement-";",like by using while or do-while loops.
Thanks
Helpful1Not Helpful0
A millisecond is 1/1000 of a second.
Thanks
Helpful1Not Helpful2
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Submit
Thanks for submitting a tip for review!
Advertisement
Warnings
If you are using the for-loop, the compiler may optimize the code, and, because the loop does nothing, remove it. This doesn't happen when using delay().
Thanks
Helpful4Not Helpful0
Note that when using the for-loop technique, you might need a very big span for i, because an empty statement is executed very fast. Such big numbers may not fit in an integer type.
Thanks
Helpful4Not Helpful1
This technique is generally useless in anything besides a trivial program. In general, use timers or an event-driven approach to implement this. Otherwise the program will become unresponsive during the delay time, and that's not always a good thing. Besides, choosing N in your loop, if it depends on instruction execution, may have surprising results. Apparently the original author has never heard of an optimizing compiler...it may optimize away the entire loop if it actually does nothing!
Thanks
Helpful4Not Helpful2
Advertisement
You Might Also Like
Advertisement
About This Article
wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 15 people, some anonymous, worked to edit and improve it over time. This article has been viewed 404,338 times.
How helpful is this?
Co-authors: 15
Updated: August 25, 2023
Views:404,338
Categories: C Programming Languages
In other languages
- Send fan mail to authors
Thanks to all authors for creating a page that has been read 404,338 times.
Is this article up to date?
Advertisement
Cookies make wikiHow better. By continuing to use our site, you agree to our cookie policy.
About This Article
Click a star to vote
% of people told us that this article helped them.
Co-authors: 15
Updated: August 25, 2023
Views:404,338
Quizzes
Do I Have a Dirty Mind QuizTake QuizAm I a Good Kisser QuizTake QuizRizz Game: Test Your RizzTake QuizWhat's Your Red Flag QuizTake QuizAm I Smart QuizTake QuizHow Insecure Am I QuizTake QuizYou Might Also Like
How to Use GCC to Compile a C Program on Linux and WindowsHow toCreate a Simple Program in C++How toEdit DLL Files in Visual StudioHow toSet Up OpenGL GLFW GLEW GLM on a Project with Visual StudioFeatured Articles
17 Pro Tips to Start Living a Healthier LifestyleHow toGet your Boyfriend to Fall in Love with YouHow toTell Your Dog NoHow toBe StylishHow toDecorate a Pumpkin Without Carving ItTrending Articles
How to Make a Fake Wound: 3 Easy MethodsHow to Pair and Use Airpods with an XboxWhat Does the Robot Emoji 🤖 Mean?How to Make Homemade Lotion: A Step-by-Step GuideFeatured Articles
Am I a Likable Person QuizHow to Improve Body Language to Send the Right MessageHow toSleep BetterHow toAsk Someone to Hang OutHow to Pick a Halloween Costume that’s Perfect for YouHow toCome Up with Good Conversation TopicsFeatured Articles
How toDeal with BracesHow to Win an "I Love You More" ArgumentHow to Make Guy FriendsHow toDeal With RumorsHow toStart a Business As a TeenagerWatch Articles
How toMake Dijon MustardHow toMake a TerrariumHow toBlow Dry Hair Without FrizzHow toArrange Flowers in a Square VaseHow toMake Homemade PopcornHow toUse Honey in Place of White SugarTrending Articles
What Does a Dragon Tattoo Mean? The Complete Cultural GuideHow toCope With Stress at SchoolHow toDecide if You Should Get Bangs or NotHow to Find Your Doppelgӓnger (or Twin)How toCompliment a Girl's PictureQuizzes
What Disney Princess Am I QuizTake QuizDo I Have a Phobia QuizTake QuizGuess My Age QuizTake QuizAm I a Genius QuizTake QuizDepression TestTake QuizWhat Human Emotion Am I QuizTake Quiz- Categories
- Computers and Electronics
- Software
- Programming
- C Programming Languages
wikiHow Newsletter
You're all set!
Helpful how-tos delivered to
your inbox every week!
Sign me up!
By signing up you are agreeing to receive emails according to our privacy policy.
- Home
- About wikiHow
- Experts
- Jobs
- Contact Us
- Site Map
- Terms of Use
- Privacy Policy
- Do Not Sell or Share My Info
- Not Selling Info
- Contribute
Follow Us
wikiHow Tech Help Pro:
Level up your tech skills and stay ahead of the curve
Let's go!
X
-
-