How To Delay In C: 7 Steps (with Pictures) - WikiHow

Skip to ContentQuizzes
  • Home
  • Random
  • Browse Articles
  • Quizzes & Games
  • All QuizzesHot
  • Love Quizzes
  • Personality Quizzes
  • Fun Games
  • Dating Simulator
  • Learn Something New
  • Forums
  • Courses
  • Happiness Hub
  • 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 How to Delay in C PDF download Download Article Everything you need to know about time delays in C language Author Info

Last Updated: November 25, 2024

PDF download Download Article
  • The "for-loop" technique
  • |
  • The "sleep()" Technique
  • |
  • Q&A
  • |
  • Tips
  • |
  • Warnings
|Show more |Show less X

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 16 people, some anonymous, worked to edit and improve it over time. This article has been viewed 412,481 times. Learn more...

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...

Delaying in C Language: Quick Tips

  • Method 1: Make your CPU work for a while without doing any other operations for a simple time delay.
  • Method 2: Use a "for" loop followed by a null statement to make a time delay in C.
  • Method 3: Use the sleep() function to specify how many milliseconds you want to program to delay and insert the code wherever you need delays.

Steps

PDF download Download Article
  1. Step 1 Make your CPU work for some time without producing any noticeable event. 1 Make your CPU work for some time without producing any noticeable event.
  2. Step 2 Do no other operation during that delay, in order to create a simple time-delay. 2 Do no other operation during that delay, in order to create a simple time-delay.
  3. Advertisement
Method 1 Method 1 of 2:

The "for-loop" technique

PDF download Download Article
  1. Step 1 Use a typical "for" loop followed by a null statement to implement time delay. 1 Use a typical "for" loop followed by a null statement to implement time delay.
  2. Step 2 Write as follows, for an example: 2 Write as follows, for an example:
    • for (i=1 ; i<100 ; i++) ;
    • The statement followed by the ";" makes the computer execute the loop 100 times without any noticeable event. It just creates a time delay.
  3. Advertisement
Method 2 Method 2 of 2:

The "sleep()" Technique

PDF download Download Article
  1. Step 1 Use sleep() The... 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. Step 2 Include the following line in your program before "int main()": 2 Include the following line in your program before "int main()":
    • #include <TIME.H>
  3. Step 3 Insert, wherever you need your program to make a delay: 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.
  4. Advertisement

Community Q&A

Search Add New Question
  • 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 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 wikiHow Yes No Not Helpful 84 Helpful 8
Ask a Question 200 characters left Include your email address to get a message when this question is answered. Submit 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 Helpful 0 Not Helpful 0
  • A millisecond is 1/1000 of a second. Thanks Helpful 0 Not Helpful 0
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 Helpful 4 Not Helpful 0
  • 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 Helpful 4 Not Helpful 1
  • 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 Helpful 4 Not Helpful 2
Advertisement

You Might Also Like

Compile a C Program Using the GNU Compiler (GCC)How to Use GCC to Compile a C Program on Linux and Windows Create a Simple Program in C++How toCreate a Simple Program in C++ Make a Countdown Program in PythonHow to Code a Python Countdown Timer: Step-by-Step Guide Delay a Batch FileHow to Delay a Batch File: Timeout, Pause, Ping, Choice & Sleep Learn to Program in CA Beginner's Guide to Programming in C Program in FortranEasy Ways to Write in Fortran Create Loops in PythonHow to Create Loops in Python Print in C2 Easy Ways to Print in C and C++ Programming Check Null in CHow toCheck Null in C Code in CHow toCode in C Make Text Blink in HTMLHow to Make Text Blink in HTML: Easy Tutorial Get Color in C ProgramHow toGet Color in C Program Create a 'Matrix' Falling Code Batch FileHow toCreate a 'Matrix' Falling Code Batch File Run Multiple Threads in JavaHow to Run Multiple Threads in Java at the Same Time 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, 16 people, some anonymous, worked to edit and improve it over time. This article has been viewed 412,481 times. How helpful is this? Co-authors: 16 Updated: November 25, 2024 Views: 412,481 Categories: C Programming Languages In other languages Spanish Italian Portuguese Russian
  • Print
  • Send fan mail to authors
Thanks to all authors for creating a page that has been read 412,481 times.

Is this article up to date?

Yes No 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 Co-authors: 16 Updated: November 25, 2024 Views: 412,481

Quizzes & Games

Cognitive TestCognitive TestTake QuizWhat Age Is My Brain QuizWhat Age Is My Brain QuizTake QuizMusic Notes & Symbols TestMusic Notes & Symbols TestTake QuizDo I Have Common Sense QuizDo I Have Common Sense QuizTake QuizImpossible English TestImpossible English TestTake QuizThe Impossible QuizThe Impossible QuizTake Quiz

You Might Also Like

Compile a C Program Using the GNU Compiler (GCC)How to Use GCC to Compile a C Program on Linux and WindowsCreate a Simple Program in C++How toCreate a Simple Program in C++Make a Countdown Program in PythonHow to Code a Python Countdown Timer: Step-by-Step GuideDelay a Batch FileHow to Delay a Batch File: Timeout, Pause, Ping, Choice & Sleep

Featured Articles

Make a Paper AirplaneHow toMake a Paper AirplaneLearn Morse CodeHow toLearn Morse CodeFold an Origami Star (Shuriken)How toFold an Origami Star (Shuriken) Fold and Use a Paper Fortune TellerHow to Fold and Use a Paper Fortune Teller

Trending Articles

Sub 5 to True Adam Chart: Looksmaxxing Tiers ExplainedSub 5 to True Adam Chart: Looksmaxxing Tiers ExplainedSigns a Woman is Sexually Attracted to YouSigns a Woman is Sexually Attracted to You28 Best Excuses for Getting Out of School28 Best Excuses for Getting Out of School Practice Thumb Pulling (And If You Should)How to Practice Thumb Pulling (And If You Should)What Female Body Shape Are You? How to Identify YoursWhat Female Body Shape Are You? How to Identify YoursDo You Agree with These "Hear Me Out" Character Hot Takes?Do You Agree with These "Hear Me Out" Character Hot Takes?

Featured Articles

What Animal Am I QuizWhat Animal Am I QuizAm I Gay QuizAm I Gay QuizHow Well Do I Know My Best Friend QuizHow Well Do I Know My Best Friend QuizAm I an Alpha, Beta, or Omega QuizAm I an Alpha, Beta, or Omega Quiz

Featured Articles

160+ Good Roasts to Burn Your Friends & Family Members160+ Good Roasts to Burn Your Friends & Family Members Play the Snaps Guessing GameHow to Play the Snaps Guessing Game140+ Wavelength Game Categories & Questions to Extend Your Play140+ Wavelength Game Categories & Questions to Extend Your PlayThe Ultimate Collection of Funny, Cheesy, & Romantic Rizz LinesThe Ultimate Collection of Funny, Cheesy, & Romantic Rizz LinesVirtual Truth or Dare Questions to Keep Things InterestingVirtual Truth or Dare Questions to Keep Things Interesting180 Good Comebacks & Savage Roasts to Win Any Argument180 Good Comebacks & Savage Roasts to Win Any Argument

Watch Articles

Eat GuavaHow toEat Guava Get Yellow Stains Out of White ShoesHow to Get Yellow Stains Out of White ShoesLeft Eye Twitching for Females: What Astrology Says About Eye TwitchingLeft Eye Twitching for Females: What Astrology Says About Eye Twitching50 First Date Conversation Starters to Spark a Connection50 First Date Conversation Starters to Spark a Connection Make a Mask Out of Paper (for Kids or Adults)How to Make a Mask Out of Paper (for Kids or Adults) Put Jibbitz on CrocsHow to Put Jibbitz on Crocs

Trending Articles

Finish the Lyrics QuizFinish the Lyrics QuizCan Your Finger Length Tell Your Personality?Can Your Finger Length Tell Your Personality?Psychopath TestPsychopath TestWhat Fruit Am I QuizWhat Fruit Am I QuizDo I Have Main Character Energy?Do I Have Main Character Energy?What Kind of Cat Am I QuizWhat Kind of Cat Am I Quiz

Quizzes & Games

When Will I Die QuizWhen Will I Die QuizTake QuizDo You Have What It Takes To Become A Millionaire?Do You Have What It Takes To Become A Millionaire?Take QuizAm I Smart QuizAm I Smart QuizTake QuizIQ TestIQ TestTake QuizHow Rich Will I Be QuizHow Rich Will I Be QuizTake QuizHow Long Will I Live QuizHow Long Will I Live QuizTake Quiz wikiHow
  • Categories
  • Computers and Electronics
  • Software
  • Programming
  • C Programming Languages
wikiHow Newsletter You're all set! Helpful how-tos delivered toyour 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 --494

Tag » How To Delay Code In C