ZetCode

Algol 68 unformatted transput

last modified November 13, 2026

Transput is the part of Algol 68 that moves values between a program and a file. The simplest operations need no format at all: print writes a row of values to the standard output, and read takes values from the standard input. This article describes those unformatted procedures, the default layout that each mode has, and the files they work with.

Writing values with print

print takes one row of values and writes the elements one after another. A string contributes its characters, and the predeclared values newline and space insert a line break and a single space.

basic.a68
BEGIN
  print(("Hello, Algol 68", newline));
  print(("one", space, "two", newline))
END

The first call writes the greeting and a line break. The second shows that space is a value like any other, so a row may mix text and directions. print itself yields VOID, which is why each call is a statement ended by a semicolon or a newline.

$ a68g basic.a68
Hello, Algol 68
one two

The default layout of each mode

Without a format, every mode has a layout chosen by the implementation. Integers and reals are padded, booleans are shortened, and the values follow each other without any separator, so the writer has to supply the spaces.

fields.a68
BEGIN
  print((1, 2, 3, newline));
  print((-42, newline));
  print((2.5, newline));
  print((TRUE, " ", FALSE, newline))
END

Each integer occupies a field eleven characters wide and is written right justified with a sign, so -42 starts after eight spaces. A real is written in exponent form with fifteen significant digits and a four character exponent field, which takes twenty-two characters. A boolean is a single T or F, and the strings in the last call are written exactly as they are.

$ a68g fields.a68
         +1         +2         +3
        -42
+2.50000000000000e  +0
T F

The write procedure

write is the procedure that print abbreviates. It takes the same row of values and writes it to the same file, with the same default layout for every mode.

write.a68
BEGIN
  write(("written with write", newline));
  write((7, newline))
END

The output is indistinguishable from the output of print, down to the eleven character field of the integer 7. The two names exist because write is the general procedure and print is the common short form, and a program may use either one.

$ a68g write.a68
written with write
         +7

Choosing the file with put

Every transput procedure takes the file as its first parameter. put(f, row) writes the row to the file f, so put(stand out, row) does exactly what print does.

put.a68
BEGIN
  put(stand out, ("written with put", newline));
  put(stand out, (2.5, newline))
END

stand out is the predeclared file for the standard output, and writing to it explicitly is how a program sends its output to the same place while keeping the destination visible in the call. The real is written in the default exponent form, because no format was given.

$ a68g put.a68
written with put
+2.50000000000000e  +0

Rows and structures as values

print also accepts a whole row or a whole structure. A row is written element by element, and a structure is written field by field in the order of its declaration.

row.a68
MODE POINT = STRUCT(INT x, INT y);

BEGIN
  [1:3] INT row := (10, 20, 30);
  print((row, newline));
  POINT p := (3, 4);
  print((p, newline))
END

The three elements are created from one initialiser and are printed with the usual integer field, which is why the numbers line up. The structure behaves in the same way: its two fields are printed one after the other. A row of characters would be printed as text, because its elements are characters.

$ a68g row.a68
        +10        +20        +30
         +3         +4

The standard files

A program starts with three open files: stand out for normal output, stand in for input, and stand error for diagnostics. print and write write to stand out, and the formatted procedure putf takes a file as well, so the same destinations serve both kinds of transput.

streams.a68
BEGIN
  put(stand out, ("direct", newline));
  putf(stand out, ($"formatted "g(0)$, 42));
  print((newline))
END

The second call passes a format denotation enclosed in dollar signs and one value, which the format turns into text. Mixing put and putf on the same file is allowed, and the last print only supplies the final line break. The next article explains the format text in detail.

$ a68g streams.a68
direct
formatted 42

A first look at printf

printf is the formatted counterpart of print. It takes a format denotation and then the values that the format consumes, and it writes the result to stand out.

preview.a68
BEGIN
  printf(($"n = "g(0)", r = "g(-8,3)$, 42, 2.5));
  print((newline))
END

The literal text of the format is copied to the output, and each g frame takes one value. The number in the frame gives the width of the field, and a negative number means that the value is right justified without a sign; the second number gives the decimals of a real. The result is a line that reads n = 42, r = 2.500.

$ a68g preview.a68
n = 42, r =    2.500

Next steps

The formatted transput article describes the format denotation, its frames, and the replicators. The reading input article shows how read and get take values from stand in. The strings article explains why a string is printed without a field, and the bits article shows the default form of a BITS value.

Source

Revised Report on the Algorithmic Language Algol 68

The Algol 68 Genie project

In this article we have printed values with and without an explicit file, looked at the default layout of integers, reals, booleans, rows, and structures, and written a first formatted line with printf. The formatted transput article continues with the format denotation and its frames.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Algol 68 tutorials.