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


ionicsachin

New Member
Disciple
Jan 19, 2009
103
6
0
30
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

New Member
Disciple
Dec 13, 2006
2,492
69
0
37
forgot c long back :( a quick peek here and there....

> #include <windows.h>

and use

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

rockyhalo

Well-Known Member
Disciple
Sep 13, 2008
125
71
98
Delhi
delay(10);

header file is #include<dos.h>

if u turbo C..

then right click and check delay for further help...
 

triple s

Well-Known Member
Disciple
Sep 30, 2008
96
0
66
30
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..........!!
 

sabby

Well-Known Member
Section Mod
Jun 13, 2009
2,335
312
173
Bangalore
@dennis

why is it ?? I still use command prompt/terminal for compiling and running C/Cpp/Java programs.
 

dennis_nail

Well-Known Member
Adept
Jun 4, 2008
651
72
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

Well-Known Member
Adept
Jun 2, 2009
360
70
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

New Member
Adept
Jun 26, 2006
683
26
0
33
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

Well-Known Member
Adept
Jun 2, 2009
360
70
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

New Member
Adept
Jun 26, 2006
683
26
0
33
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.
 

red_devil

New Member
Disciple
Feb 20, 2009
116
0
0
keeping away from the compiler war, why not just use

system("pause") - and get on with it?
 

blufox

Well-Known Member
Adept
Jun 2, 2009
360
70
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.
 

a_k_s_h_a_y

Active Member
Disciple
Aug 1, 2007
432
16
31
31
www.akshayy.com
basically the point was TurboC is not Standard C or ANSI C! Period. So newbs should switch to GCC or MSVC. Or some new compiler after 2001 at least.