Monday, May 24, 2010

How do I use C to program a delay?

Just for example. How would I get the programme to say:





printf("blah blah");





Then delay here for a set length of time





printf("blah blah blah");





And so on..

How do I use C to program a delay?
on a unix system you would call sleep(seconds);
Reply:Why don't you join one of the hundreds of C forums or user groups where answers to your question will continue for weeks. You will learn far more from these groups than by asking a single question here. That's how I learned to program.
Reply:Here's one way. The time function is found in (at least) DOS, Unix, Win16, Win32, ANSI C, ANSI C++ and OS/2.





Your compiler may offer a better function. For instance, on a DOS, Unix, Win32 or OS/2 system, you could use sleep(), but that's not ANSI C. What I'm offering here uses up a lot of CPU cycles, so it would be OK on an individual PC, but it would not be very acceptable on a multiuser system such as a server.





#define DELAY 4


#include %26lt;time.h%26gt;


#include %26lt;stdio.h%26gt;





int main(void)


{


time_t begin, end;





end = begin = time(NULL);


printf("blah blah");


while ((end - begin) %26lt; DELAY)


{


end = time(NULL);


}


printf("blah blah blah");





return 0;


}
Reply:There is a function in C that works for delaying or sleeping for a certain amount of time....most times, this time is specified in microseconds (i think so...its been long since i used C).





delay( )


The delay( ) function found in dos.h is processor dependent. And it won't work on all


systems. The reason is the delay function is implemented with clock speed.





Simply try writting "delay(400);" It might just work.





Dont forget to include "dos.h", thats the header file for delay function.
Reply:#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;dos.h%26gt;





main()


{


printf("blah blah");


delay(1500);


printf("blah blah blah");


getch();


}


No comments:

Post a Comment