Is there any way to replace system("pause")?
Is there any way to replace system("pause")?
Is my solution works fine without affect my other input?Code:#include <stdio.h> int main() { printf("Press any key to continue..."); getchar(); return 0; }
where is input buffer goes?
That is what I used in Turbo C, to hold the console window open and give me a chance to read the output of the program, before it closed.
Won't bother anything. Input from your getchar() will go STRAIGHT to byte heaven, and sing thereafter with the byte (little) Angels.
In standard C, no. Window's pause command exits as soon as a keystroke is detected, and standard C provides no means to get keyboard keystrokes directly.
The commonly used alternative is to use a function like getchar() to read a character. Bear in mind that that getchar() will not return until the user hits the enter key (input is buffered until then). You will probably need to call it twice if preceding code uses scanf().
The sensible way is not to run the program from inside an IDE - run it directly using a manually launched command prompt.
1) Why getchar()needs to call twice if preceding code uses scanf()?
2) How to design to achieve Press any key to continue...?
scanf leaves a newline char behind. So if you don't have 2 getchar(), the newline will be grabbed by getchar(), and your program automatically quits.
Does your compiler have the non-standard header <conio.h>?
If you do have this non-standard header, you may use the function getch(), which gets a character from stdin without waiting for enter to be hit.
Fact - Beethoven wrote his first symphony in C