简体   繁体   中英

How to pause in C?

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?

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

Is it a console program, running in Windows? If so, run it from a console you've already opened. ie run "cmd", then change to your directory that has the .exe in it (using the cd command), then type in the exe name. Your console window will stay open.

Under POSIX systems, the best solution seems to use:

#include <unistd.h>

pause ();

If the process receives a signal whose effect is to terminate it (typically by typing Ctrl + C in the terminal), then pause will not return and the process will effectively be terminated by this signal. A more advanced usage is to use a signal-catching function, called when the corresponding signal is received, after which pause returns, resuming the process.

Note: using getchar() will not work is the standard input is redirected; hence this more general solution.

你也可以只使用system("pause");

如果你正在制作一个控制台窗口程序,你可以使用 system("pause");

getch() can also be used which is defined in conio.h.

The sample program would look like this :

#include <stdio.h>
#include <conio.h>

int main()
{
    //your code 
    getch();
    return 0; 
} 

getch() waits for any character input from the keyboard (not necessarily enter key).

Good job I remembered about DOS Batch files. Don't need Getchar() at all. Just write the batch file to change directory (cd) to the folder where the program resides. type the name of the exe program and on the next line type pause. example:

cd\

wallpaper_calculator.exe pause

I assume you are on Windows. Instead of trying to run your program by double clicking on it's icon or clicking a button in your IDE, open up a command prompt, cd to the directory your program is in, and run it by typing its name on the command line.

I'm not the best at C and I'm kind of new; but here's what I would do.

If you want to press any key to continue/end the program. I'd create a void function pause().

 #include <stdlib.h>
 #include <stdio.h>
    void pause() {
        system("pause>nul");
        return;
    }
    
    int main() {
        //your code here
        pause(); // you pause here
    }

For time delays between code, you can use sleep(), sleep(1) = 1-second delay, and so on.

For Linux; getchar() is all you need.

If you are on Windows, check out the following, it is exactly what you need!

  • kbhit() function is used to determine if a key has been pressed or not.
  • To use kbhit() in C or C++ prorams you have to include the header file "conio.h".

For example, see how it works in the following program;

//any_key.c

#include <stdio.h> 
#include <conio.h>

int main(){

    //code here
    printf ("Press any key to continue . . .\n");
    while (1) if (kbhit()) break; 
    //code here 
    return 0;

}

When I compile and run the program, this is what I see.

Only when user presses just one key from the keyboard, kbhit() returns 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM