I am a beginner of C. I run the C program, but the window closes too fast before I can see anything. How can I pause the window?
The pause() function shall suspend the calling thread until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process. If the action is to terminate the process, pause() shall not return.
How can I drop a delay for half a second? In the C language, sleep() accepts integers that represent the number of milliseconds the program should wait, which means you need to call sleep(500) to wait half a second.
3 Answers. Press Control + Z . This will suspend the process and return you to a shell.
The delay() function is built upon a C library function called clock(). The clock() function returns a time value in clock ticks, which is based on the processor's speed. The value returned is of the clock_t variable type. You can use subsequent reads of the clock() function to determine elapsed time.
you can put
getchar();
before the return from the main function. That will wait for a character input before exiting the program.
Alternatively you could run your program from a command line and the output would be visible.
If you want to just delay the closing of the window without having to actually press a button (getchar()
method), you can simply use the sleep()
method; it takes the amount of seconds you want to sleep as an argument.
#include <unistd.h> // your code here sleep(3); // sleep for 3 seconds
References: sleep() manual
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With