How to pause in C?

后端 未结 10 847
清歌不尽
清歌不尽 2020-12-05 18:11

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?

相关标签:
10条回答
  • 2020-12-05 18:50

    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

    0 讨论(0)
  • 2020-12-05 18:53

    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.

    0 讨论(0)
  • 2020-12-05 18:53

    You could also just use system("pause");

    0 讨论(0)
  • 2020-12-05 18:53

    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

    0 讨论(0)
  • 2020-12-05 18:55

    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).

    0 讨论(0)
  • 2020-12-05 18:56

    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.

    0 讨论(0)
提交回复
热议问题