Sung Min Kim Sung Min Kim - 2 months ago 10
C Question

"Press Any Key to Continue" function in C

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

What I want to do is:

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
//The Void Function Here
//Then I will call the function that will start the game

Answer

Use C standard library function getchar() ,getch() is boreland function not standard one.

only used in windows TURBO C.

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

you should press return key Here. for this the printf statement should be press ENTER to continue.

if you press a , then again you need to press ENTER.
if you press ENTER it will continue normally.

for this reason, it should be

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.