3.5 KiB
Input/Output
In programming input/output (I/O or just IO) refers to communication of a computer program with the outside environment, for example with the user in real world or with the operating system. Input is information the program gets from the outside, output is information the program sends to the outside. I/O is a basic and very important term as it separates any program to two distinct parts: the pure computational system (computation happening "inside") and I/O which interconnects this system with the real world and hence makes it useful -- without I/O a program would be practically useless as it couldn't get any information about the real world and it couldn't present computed results. In hardware there exists the term "I/O device", based on the same idea -- I/O devices serve to feed input into and/or get output from a physical computer, for example keyboard is an input device and monitor is an output device (a computer without I/O devices would be useless just as a program without I/O operations).
Note that I/O is not just about communication with a human user, it also means e.g. communication over network, reading/writing from/to files etc.
It is possible to have no input (e.g. a demo), but having no output at all probably makes no sense (see also write-only).
I/O presents a challenge for portability! While the "pure computation" part of a program may be written in a pure platform-independent language such as C (and can therefore easily be compiled on different computers), the I/O part of the program usually requires some platform specific library or a library with many dependencies; for example to display pictures on screen one may use SDL, OpenGL, Linux framebuffer, CSFML, X11, Wayland and many other libraries, each one handling I/O a bit differently. Whatever library you choose, it may be unavailable on some other platform, so the program won't run there. Some hardware platforms (e.g. many game consoles) even have their own exclusive I/O library, use of which will just tie the program to that single platform. There are programming languages and libraries that try to provide platform-independent I/O, but such approach is limited as it has to suppose some common features that will be available everywhere; for example C has a standard platform-independent I/O library stdio, but it only allows text input/output, for anything advanced such as graphics, sound and mouse one has to choose some 3rd party library. Unix philosophy also advises to only use text I/O if possible, so as to "standardize" and tame I/O a bit, but then again one has to choose what communication protocol/format to use etc. So generally I/O is a problem we have to deal with.
How to solve this? By separating I/O code from the "pure computation" code, and by minimizing and abstracting the I/O code so that it is easily replaceable. Inexperienced programmers often make the mistake of mixing the pure computation code with I/O code -- it is then very difficult to replace such I/O code with different I/O code on a different platform. See portability for more detail.
I/O also poses problems in some programming paradigms, e.g. in functional programming.
TODO: code example