How to put a delay in execution in a C program?

ionicsachin

Disciple
Jan 19, 2009
103
6
0
34
Hiii,
I wanted to know how to put a delay of milliseconds or seconds at any step in a C program. On googling i came across sleep(), but its not a standard C funtion. Any suggestions?
 

thetechmind

Skilled
Dec 13, 2006
2,492
69
0
41
forgot c long back :( a quick peek here and there....

> #include <windows.h>

and use

> Sleep(ms). *note the capital S*
 

triple s

Disciple
Sep 30, 2008
96
0
66
34
www.google.com
There is inbuilt function delay().

e.g. delay(3);

And you are suppossed to include the header file #include<dos.h> before.........

Edit:

rockyhalo beats me to it..........!!
 

dennis_nail

Adept
Jun 4, 2008
651
73
117
^^ It is surprising for me because when I was in college more than a decade ago, we used to use Turbo C. I suppose there would be better options now. But at that time, what tools we used was controlled by Lab Assistants. We had to use whatever was easier for them to install on Lab PCs.
 

blufox

Adept
Jun 2, 2009
360
72
67
Mountain View, CA
Rule of thumb :- Anything which is dependent on hardware is not std C.

So putting a sleep or delay in std C is not possible.

You ought to use platform specific C api e.g Unix C api or Windows api.

Which platform are you stuck at?

Search in your msdn docs/man pages on how to use sleep for your platform.

PS:- If you are using TurboC, throw that junk, we have moved to 64 bits processors which is evolution, why still live in 16 bit world? :)
 

hammerhead

Adept
Jun 26, 2006
683
26
0
37
www.teamreflex.org
^I am tired of people saying dump TurboC etc. Sure TurboC may be outdated, I still find it pretty reliable.

My advise is that if you want to go by the Industry and syntax and want to become a C/C++ coder, dump TurboC.

If you want to focus on algorithms and become a better problem solver then keep the compiler you work on even if its TurboC. It will not even matter, the least you need is paper, pen and maths.

I would rather start with a Compiler with minimal libraries rather than a advanced one where everything is just an API call away.
 

blufox

Adept
Jun 2, 2009
360
72
67
Mountain View, CA
hammerhead said:
^I am tired of people saying dump TurboC etc. Sure TurboC may be outdated, I still find it pretty reliable.

My advise is that if you want to go by the Industry and syntax and want to become a C/C++ coder, dump TurboC.

If you want to focus on algorithms and become a better problem solver then keep the compiler you work on even if its TurboC. It will not even matter, the least you need is paper, pen and maths.

I would rather start with a Compiler with minimal libraries rather than a advanced one where everything is just an API call away.

Your finding it reliable does not means it is the correct way.

First and foremost problem TurboC is not Ansi Complaint, which makes the point of algorithm development mute.

Sure you need a general maths, algos , pencil and paper but what you need min to implement this is an ANSI complaint compiler.

It is not about becoming Industry standard coder(there is no standard BTW), it is more about getting the very basic fundamentals of C programming language correct. If you get it right in the initial stages it really helps you.

And if people keep using TurboC because they are supposed to learn Algos not correct C programming then god help them.

Just imagining what will happen if some student happens to go for a masters with his elite TurboC skills. :(

Flamebait Alert :- Same goes for Yashwant Kanetkar's Let Us C Vs any other good C programming Book.
 

hammerhead

Adept
Jun 26, 2006
683
26
0
37
www.teamreflex.org
First and foremost problem TurboC is not Ansi Complaint, which makes the point of algorithm development mute.
How can you even relate designing Algorithms to a specific compiler. Algorithms existed way before there were even computers. That meams choosing a non ANSI complaint compiler will have no effect on by designing skills.

Pick up a good book such as Cormen, and nowhere will you find something written specific for a compiler.

And if people keep using TurboC because they are supposed to learn Algos not correct C programming then god help them.

Just imagining what will happen if some student happens to go for a masters with his elite TurboC skills.

Again, why? Like I said, Turbo C offers everything you need you good Algorithms designs. I have implemented tons the Algorithms in Cormen in Turbo C without any problems.

I have done lots of coding in C/C++ on g++, miniGW, TurboC and WinAVR. Surely compilers did not change the way I think.
 

blufox

Adept
Jun 2, 2009
360
72
67
Mountain View, CA
hammerhead said:
First and foremost problem TurboC is not Ansi Complaint, which makes the point of algorithm development mute.
How can you even relate designing Algorithms to a specific compiler. Algorithms existed way before there were even computers. That meams choosing a non ANSI complaint compiler will have no effect on by designing skills.

Pick up a good book such as Cormen, and nowhere will you find something written specific for a compiler.

And if people keep using TurboC because they are supposed to learn Algos not correct C programming then god help them.

Just imagining what will happen if some student happens to go for a masters with his elite TurboC skills.

Again, why? Like I said, Turbo C offers everything you need you good Algorithms designs. I have implemented tons the Algorithms in Cormen in Turbo C without any problems.

I have done lots of coding in C/C++ on g++, miniGW, TurboC and WinAVR. Surely compilers did not change the way I think.

Oh God, looks like I am giving out the wrong impression to you :).

It is about learn the right way to do it.

Alogrithm does not need a compiler, I know but you do code to implement it, to see the real time running complexities right? Off course unless you are a theoretical computer scientist :).

Certainly you will not spoil your algo skills with TurboC, I agree but you will pick non standard tricks which defeats the very purpose of C programming language, writing a portable code.

Or I am in company of people who have nearly very good ANSI C complaint programming skills, that I am not used to non std C code.