How to pause in C?

后端 未结 10 842
清歌不尽
清歌不尽 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 19:06

    For Linux; getchar() is all you need.

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

    kbit() function

    • 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  
    #include 
    
    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.

提交回复
热议问题