Programming in D – Tutorial and Reference
Ali Çehreli

Other D Resources

Standard Input and Output Streams

So far, the printed output of our programs has been appearing on the terminal window (or screen). Although the terminal is often the ultimate target of output, this is not always the case. The objects that can accept output are called standard output streams.

The standard output is character based; everything to be printed is first converted to the corresponding character representation and then sent to the output as characters. For example, the integer value 100 that we've printed in the last chapter is not sent to the output as the value 100, but as the three characters 1, 0, and 0.

Similarly, what we normally perceive as the keyboard is actually the standard input stream of a program and is also character based. The information always comes as characters to be converted to data. For example, the integer value 42 actually comes through the standard input as the characters 4 and 2.

These conversions happen automatically.

This concept of consecutive characters is called a character stream. As D's standard input and standard output fit this description, they are character streams.

The names of the standard input and output streams in D are stdin and stdout, respectively.

Operations on these streams normally require the name of the stream, a dot, and the operation; as in stream.operation(). Because stdin's reading methods and stdout's writing methods are used very commonly, those operations can be called without the need of the stream name and the dot.

writeln that we've been using in the previous chapters is actually short for stdout.writeln. Similarly, write is short for stdout.write. Accordingly, the hello world program can also be written as follows:

import std.stdio;

void main() {
    stdout.writeln("Hello, World!");
}
Exercise

Observe that stdout.write works the same as write.