How To Delay In C: 7 Steps (with Pictures) - WikiHow
Maybe your like
- Log in / Sign up
- The "for-loop" technique |
- The "sleep()" Technique |
- Q&A |
- Tips |
- Warnings
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
-
1 Make your CPU work for some time without producing any noticeable event. -
2 Do no other operation during that delay, in order to create a simple time-delay. Advertisement
The "for-loop" technique
-
1 Use a typical "for" loop followed by a null statement to implement time delay. -
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.
Advertisement
The "sleep()" Technique
-
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 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 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
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
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
You Might Also Like
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- Send fan mail to authors
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,481Quizzes & Games
You Might Also Like
Featured Articles
Trending Articles
Featured Articles
Featured Articles
Watch Articles
Trending Articles
Quizzes & Games
- Categories
- Computers and Electronics
- Software
- Programming
- C Programming Languages
- 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 --Tag » How To Delay Code In C
-
Time Delay In C - GeeksforGeeks
-
Delay Function In C | Programming Simplified
-
How Do You Use The Time Delay In C Programming? - Quora
-
The Delay() Function | C For Dummies Blog
-
Implement Time Delay In C - Stack Overflow
-
5 Techniques To Delay Code Execution | Beningo Embedded Group
-
C Examples - Time Delay | ArunEworld
-
C Programming - How To Implement And Use Delays - YouTube
-
C Delay Code Example - Code Grepper
-
C - How To Create Delay Function According To Program Need?
-
How To Use Delay Option In C Programing In Code Block - Sololearn
-
Time Delay Loops
-
How To Wait For Seconds In C++? - Java2Blog
-
Delay Routine Function In C Code | All About Circuits