Pregunta

I'm beginner in C programming. When I programmed in Windows using any Compiler then to hold the screen I used getch().

Without it when I run the program, the screen suddenly disappears. But when I did same thing in Linux, there is no need of getch(). When I run my program, it runs and screen does not disappear.

Why is the screen (terminal) in Linux is not closed like in Windows?

¿Fue útil?

Solución

A console window will be closed by whatever opened it in the first place.

In both Windows and Linux, you can launch a terminal emulator window and specify what program will run in that window. When that program terminates, the window will close (unless you specify some option to keep it open).

Windows software development is typically done from an IDE such as Visual Studio. The usual way to launch a program from Windows opens a terminal window that just executes your program. The window closes when the program finishes -- thus the need for getch() or something similar. (Note that the getch() function is specific to MS Windows.)

On Unix/Linux systems, you typically launch a terminal window from the desktop manager, and the program running in the window is an interactive shell. If you run a command from the shell, the window remains open when the program finishes; it remains open until the shell terminates (when you type exit or Ctrl-D at a shell prompt.

Both methods can be used on either system. On Windows, you can launch a console window running cmd.exe, which gives you a C:\ prompt; you can then run commands from that prompt, and the window will remain open after the command terminates. Conversely, on Unix or Linux, you can launch a window that just runs one command, for example xterm -e ./my_program &, and the window will close when the program finishes.

The difference is that Windows, and Windows development environments, place a greater emphasis on GUI (graphical) programs. Support for console programs (programs that write output to stdout) is almost an afterthought (though support for such programs is much older). But console programs tend to be easier to write, and beginning programmers are more likely to develop them.

UNIX-like systems, on the other hand, still place a much greater emphasis on programs that write to standard output (though there are also plenty of GUI programs).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top