27

For example could I make it type something like

"Hello"
"This"
"Is"
"A"
"Test"

With 1 second intervals in-between each new line?

Thanks,

Levon
  • 138,105
  • 33
  • 200
  • 191
AppleAssassin
  • 465
  • 1
  • 5
  • 10
  • I've tried nothing, I'm new to C So I don't know all commands I can use...Was just wondering if there was a command to wait a certain time before doing something else – AppleAssassin Jun 06 '12 at 22:03

5 Answers5

55

Well the sleep() function does it, there are several ways to use it;

On linux:

#include <stdio.h>
#include <unistd.h> // notice this! you need it!

int main(){
    printf("Hello,");
    sleep(5); // format is sleep(x); where x is # of seconds.
    printf("World");
    return 0;
}

And on windows you can use either dos.h or windows.h like this:

#include <stdio.h>
#include <windows.h> // notice this! you need it! (windows)

int main(){
    printf("Hello,");
    Sleep(5); // format is Sleep(x); where x is # of milliseconds.
    printf("World");
    return 0;
}

or you can use dos.h for linux style sleep like so:

#include <stdio.h>
#include <dos.h> // notice this! you need it! (windows)

int main(){
    printf("Hello,");
    sleep(5); // format is sleep(x); where x is # of seconds.
    printf("World");
    return 0;
}

And that is how you sleep in C on both windows and linux! For windows both methods should work. Just change the argument for # of seconds to what you need, and insert wherever you need a pause, like after the printf as I did. Also, Note: when using windows.h, please remember the capital S in sleep, and also thats its milliseconds! (Thanks to Chris for pointing that out)

Rivasa
  • 6,510
  • 3
  • 35
  • 64
5

something not as elegant as sleep(), but uses the standard library:

/* data declaration */
time_t start, end;

/* ... */

/* wait 2.5 seconds */
time(&start);
do time(&end); while(difftime(end, start) <= 2.5);

I'll leave for you the finding out the right header (#include) for time_t, time() and difftime(), and what they mean. It's part of the fun. :-)

CST
  • 69
  • 1
  • 3
    Your suggestion **also** needlessly wastes processor cycles. sleep() or some variant is the right answer. – pb2q Jun 06 '12 at 22:35
  • 1
    Oh, you are right. Is not elegant (as I stated already). It's just portable. But wasting processor cycles is not just a side effect. Actually is the main idea behind the code, is what makes it work, clumsily as you may say. :-) – CST Jun 06 '12 at 22:43
2

You can look at sleep() which suspends the thread for the specified seconds.

P.P
  • 117,907
  • 20
  • 175
  • 238
-1

The most easiest way is to give a loop. Be it while or for loop

int main()
{
while(i<100000) //delay
{
 i++;     
 }
 }
  • 2
    This gives little control over the delay length. In fact any decent optimising compiler will just remove the loop, as having no nett effect. – Adrian Mole Jun 24 '22 at 17:18
  • One can also use conditional while loop for delay, In case the code is waiting for any event to happen and require delay. Then simply use `While(!event);` it will become false only when event come, otherwise will wait indefinitely. – RAJ KUMAR NAYAK Jul 06 '22 at 04:31
-8

Works on all OS

int main()
{
char* sent[5] ={"Hello ", "this ", "is ", "a ", "test."};
int i =0;
while( i < 5 )
{
printf("%s", sent[i] );
int c =0, i++;
while( c++ < 1000000 ); // you can use sleep but for this you dont need #import
} 
return 0;
}
Eveler
  • 177
  • 2
  • 9
  • 11
    This is a very bad practice. Your loop just needlessly wastes processor cycles. Why should you be averse to using `#import` for a standard sleep function? Worst case you'll need to #ifdef to cover multiple platforms. – pb2q Jun 06 '12 at 22:33
  • Its just an Option if you sayits bad practice so i spend my ten years for nothing – Eveler Jun 06 '12 at 22:36
  • 6
    If you don't understand why this is a bad idea then you should learn why now. – pb2q Jun 06 '12 at 22:37
  • Ithink that if app use 1 sleep or 1 while for todays cpu is like do nothing.So its not big deal as i said itsan option and I dont give bad advices. – Eveler Jun 06 '12 at 22:41
  • while is natural function. sleep() I dont use it , coz i think its for ppl who not much understand whats going on. – Eveler Jun 06 '12 at 22:47
  • I think you use function as strcmp, strcpy. But i use my own scmp, scpy function, I can edit it, do what i want, change, do you can change strcmp? no bcoz you use original .. in c/c++ I creating and testing my own function if i need i use asm. so i can make wat ever i want this is a point of programming. – Eveler Jun 06 '12 at 22:56
  • 4
    Late to the downvote party, but this solution also presumes that one million cycles of the while loop take one second. So while this solution will work on any operating system with a C compiler (not *all OS*), it is not portable across architectures. `sleep()` and `difftime()` both take [CLOCKS_PER_SEC](https://www.gnu.org/software/libc/manual/html_node/CPU-Time.html) into account. – Matthew Cole Sep 08 '17 at 19:08
  • 4
    Also, it's pretty weird that you're averse to including `sys/wait.h`, `unistd.h`, `time.h` or `string.h` for standard library functions, but you're cool with including `stdio.h` for `printf(...)`. – Matthew Cole Sep 08 '17 at 19:10
  • @Eveler it may be good for a beginner to understand different ways of using fundamentals like while loops to create a delay but on computational scale it's like wasting your CPU's efficiency, while working on time critical environments you will see a difference. But its been 7 years since you posted this so I guess you already know it now :p – Abhay Nayak May 22 '19 at 14:17