“Press Any Key to Continue” function in C

后端 未结 5 1333
無奈伤痛
無奈伤痛 2021-01-30 14:15

How do I create a void function that will work as \"Press Any Key to Continue\" in C?

What I want to do is:

printf(\"Let the Battle Begin!\\n\");
printf(         


        
5条回答
  •  孤街浪徒
    2021-01-30 14:44

    Use the C Standard Library function getchar() instead as getch() is not a standard function, being provided by Borland TURBO C for MS-DOS/Windows only.

    printf("Let the Battle Begin!\n");
    printf("Press Any Key to Continue\n");  
    getchar();    
     
    

    Here, getchar() expects you to press the return key so the printf statement should be press ENTER to continue. Even if you press another key, you still need to press ENTER:

    printf("Let the Battle Begin!\n");
    printf("Press ENTER key to Continue\n");  
    getchar();    
    

    If you are using Windows then you can use getch()

    printf("Let the Battle Begin!\n");
    printf("Press Any Key to Continue\n");
    getch();   
    //if you press any character it will continue ,  
    //but this is not a standard c function.
    

    char ch;
    printf("Let the Battle Begin!\n");
    printf("Press ENTER key to Continue\n");    
    //here also if you press any other key will wait till pressing ENTER
    scanf("%c",&ch); //works as getchar() but here extra variable is required.      
    

提交回复
热议问题