Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause in C?

Tags:

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?

like image 497
CathyLu Avatar asked Feb 02 '11 00:02

CathyLu


People also ask

What is pause () used for?

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 do you make a C program pause for a few seconds?

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.

How do you pause a running code?

3 Answers. Press Control + Z . This will suspend the process and return you to a shell.

Is there a delay function in C?

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.


2 Answers

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.

like image 83
Gavin H Avatar answered Oct 08 '22 14:10

Gavin H


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

like image 37
George Avatar answered Oct 08 '22 15:10

George