Thread: delay function in C in number of seconds

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    delay function in C in number of seconds

    Hi.

    I study C. I need a function or something that will cause delay of given number of seconds, like delay(20) would wait 20 seconds before continuing.

    How can I do that in C language?

    Thank you.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The techniques to do that are operating system dependent. There is no standard function in the C library to do it.

    Under most unix operating systems, there is a function named sleep() that accepts an argument in seconds. It is in a non-standard header <unistd.h>, I think.

    Under windows, look up the win32 API function Sleep(). It accepts an argument in milliseconds.

    A portable approach is to loop and check the time (using time() or a similar function) until 20 seconds have passed. That is not generally a good idea though, as your program consumes CPU cycles while "delayed" - which is not a good idea, as it affects ability of other applications or the operating system itself to run.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    In Unix, you can use unistd.h's sleep() function, as grumpy said, and you can use it for the Windows API, too. Here's a delay function I found, that uses the C standard library:

    Code:
    #include <time.h>
    
    void wait(float x) {
    	time_t start;
    	time_t current;
    	time(&start);
    	do
    		time(&current);
    	while (difftime(current,start) < x);
    }
    Please note that this is not the best method, and it consumes a lot of memory.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Babkockdood View Post
    Please note that this is not the best method, and it consumes a lot of memory.
    Your method is roughly what I was referring to at the end of my previous post.

    It tends to consume CPU cycles though, not memory.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It's probably best to use the OS specific sleep() or Sleep() as these include code that does not race the CPU. Using the loop checking diftime() can cause moments of very high CPU usage... long waits can result in heat problems for poorly cooled systems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running a function for a period of seconds
    By Akkernight in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2009, 12:41 PM
  2. Is there a delay function?
    By Digga in forum Windows Programming
    Replies: 2
    Last Post: 05-06-2005, 10:56 AM
  3. Time delay function
    By sundeeptuteja in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2003, 05:17 AM
  4. Delay function
    By knight543 in forum C++ Programming
    Replies: 4
    Last Post: 01-14-2002, 03:04 PM
  5. Use a delay function
    By Robert_Ingleby in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2001, 06:58 AM