less_retarded_wiki/io.md
2023-05-28 12:54:45 +02:00

15 lines
3.5 KiB
Markdown

# Input/Output
In [programming](programming.md) input/output (I/O or just IO) refers to communication of a computer [program](program.md) with the outside environment, for example with the [user](user.md) in [real world](irl.md) or with the [operating system](os.md). Input is [information](information.md) 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](computational_system.md) (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](hardware.md) 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](computer.md), 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](network.md), reading/writing from/to [files](file.md) etc.
It is possible to have no input (e.g. a [demo](demoscene.md)), but having no output at all probably makes no sense (see also [write-only](write_only.md)).
**I/O presents a challenge for [portability](portability.md)!** While the "pure computation" part of a program may be written in a pure platform-independent language such as [C](c.md) (and can therefore easily be compiled on different computers), the I/O part of the program usually requires some platform specific [library](library.md) or a library with many [dependencies](dependency.md); for example to display pictures on screen one may use [SDL](sdl.md), [OpenGL](opengl.md), Linux framebuffer, CSFML, [X11](x11.md), [Wayland](wayland.md) 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](c.md) 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](unix_philosophy.md) 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](protocol.md)/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](abstraction.md) 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](portability.md) for more detail.
I/O also poses problems in some programming [paradigms](paradigm.md), e.g. in [functional programming](functional.md).
TODO: code example