Jump to content

delay function in C in number of seconds

- - - - -

  • Please log in to reply
4 replies to this topic

#1
nerio

nerio

    Newbie

  • Members
  • PipPip
  • 14 posts
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.
I am a student of C language.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
What is your operating system and compiler? There is no simple way in plain C, however you may utilize an API to better this.

With C in Windows, you can always include windows.h and use the Sleep(milliseconds) function, in POSIX systems it will be unistd.h and sleep(seconds).

Sleep Function (Windows)
sleep (opengroup)

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
If you are really on same platform which does not have a standard API, you can still do some experiment and based upon processor speed you can write code to do the job.

A little wild idea.


for(int i=0; i<1000000000; i++);


The above takes 3.2 - 3.4 seconds on my pc using codeblocks.
Today is the first day of the rest of my life

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Maybe I would do a while loop and compare time (assuming time.h is provided on that system) - however this would be harsh on the CPU for more than a moment.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Maybe good old clock() will do the trick.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users