When you try to start a file by double-clicking on it, the application runs and the console window immediately collapses.
When the process for which the console window was created, completes its work, the operating system immediately destroys the window . getch() you simply “freeze” the program while waiting for the entered character, thereby delaying its completion.
How does this happen? When starting the program, the PE EXE loader looks at the value of its “subsystem” field and, seeing the WINDOWS_CUI value WINDOWS_CUI , requests the console subsystem ( conhost.exe ) to create a window. Then the streams of this window are bound to standard I / O streams. Further, conhost monitors the status of these threads.
If this process starts up some other processes and passes the already existing I / O handles to them, the console subsystem will not worry about their completion. For example, if we run cmd , and from there your program, then the completion of this program will not result in closing the window . But if you complete the cmd itself (using the exit , for example), the console will instantly close.
PS: I do not know the motives for such behavior, exactly as the reasons for the impossibility of forcing the phrase “The program has completed its work. Press any key to close the window by the console subsystem. Just take it as given.
getchar()makes the exe-file not launch by double-clicking ... You do not finish speaking something. - HarryC? I assure you that programs written in Pascal, assembler or Perle behave the same way. - PinkTuxgetchar()just stops the program execution while waiting for input with pressing the Enter key, and has nothing to do with the number of clicks to start ... - Harry