ZetCode

Ada text output

last modified November 23, 2026

Ada writes text through the package Ada.Text_IO. The basic operations are Put, which writes without ending the line, Put_Line, which writes and ends it, and New_Line, which ends one or more lines. For numbers the package is less convenient, because each numeric type needs its own instance of a generic package: Integer_IO, Float_IO, or Enumeration_IO. Those instances add control over width, base, decimal places, and letter case.

Put, Put_Line and New_Line

Put_Line has an overload for String, while Put has overloads for both strings and single characters. That makes it possible to build a line from several pieces and to place a space or another character exactly where it is needed.

put_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Put_Demo is
begin
   Put ("Hello there!");
   New_Line;
   Put ("one");
   Put (' ');
   Put ("two");
   New_Line (2);
   Put_Line ("done");
end Put_Demo;

The first line ends with New_Line, and the character ' ' separates one and two without a newline. New_Line (2) ends two lines, which leaves one empty line between one two and done. Passing a Character to Put_Line is not allowed, because that procedure expects a string.

$ gnatmake -gnat2022 -gnatwa put_demo.adb
$ ./put_demo
Hello there!
one two

done

Integer output with a width

To format an integer, the program instantiates Ada.Text_IO.Integer_IO for the type. The resulting Put takes an optional Width, which is the minimum number of characters for the field. A positive number is right-aligned in that field and padded with spaces on the left.

integer_columns.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Integer_Columns is
   package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
   use Int_IO;

   Values : constant array (1 .. 4) of Integer := [7, 42, 1_000, -15];
begin
   for I in Values'Range loop
      Put (Values (I), Width => 6);
      New_Line;
   end loop;
end Integer_Columns;

Each value occupies six columns, so the last digits line up: 7 ends in column 6, 42 in columns 5 and 6, and so on. Unlike Integer'Image, Int_IO.Put does not add a leading blank of its own, so the width is the complete field. A value that needs more characters than the width is printed in full; the width is a minimum, not a limit.

$ gnatmake -gnat2022 -gnatwa integer_columns.adb
$ ./integer_columns
     7
    42
  1000
   -15

Printing in another base

The same Put accepts a Base parameter for bases from 2 to 16. The output is written in Ada literal form, with the base, a #, the digits, and a closing #. Hexadecimal digits appear in upper case.

bases.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Bases is
   package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
   use Int_IO;
begin
   Put (255, Width => 10, Base => 16);
   New_Line;
   Put (10, Width => 10, Base => 2);
   New_Line;
   Put (64, Width => 6, Base => 8);
   New_Line;
end Bases;

255 is 16#FF#, 10 is 2#1010#, and 64 is 8#100#. The width still applies to the whole literal, including the base and the hash marks. This form is useful for diagnostics and for writing values that another Ada program can read back with a based literal.

$ gnatmake -gnat2022 -gnatwa bases.adb
$ ./bases
    16#FF#
   2#1010#
8#100#

Float output

Real values use Ada.Text_IO.Float_IO. Its Put takes three parameters that describe the field: Fore is the minimum number of digits before the decimal point, Aft is the number of digits after it, and Exp is the number of exponent digits. Setting Exp to zero selects ordinary fixed-point notation, and the value is rounded to Aft places.

float_columns.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Float_Columns is
   package Float_IO is new Ada.Text_IO.Float_IO (Float);
   use Float_IO;
begin
   Put (3.14159, Fore => 2, Aft => 3, Exp => 0);
   New_Line;
   Put (2.5, Fore => 1, Aft => 2, Exp => 0);
   New_Line;
   Put (12345.678, Fore => 3, Aft => 1, Exp => 0);
   New_Line;
end Float_Columns;

The first value is rounded to 3.142 in a field with at least two digits before the point. The second becomes 2.50, with the trailing zero kept because Aft fixes the number of decimal places. The third needs five digits before the point, so Fore is exceeded and the field grows to fit 12345.7.

$ gnatmake -gnat2022 -gnatwa float_columns.adb
$ ./float_columns
 3.142
2.50
12345.7

Enumeration output

An enumeration type gets its own Enumeration_IO instance. Its Put accepts a width and a Set parameter, which chooses upper or lower case for the literal names. Enumeration fields are left-aligned, so the padding goes after the name.

enum_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Enum_Demo is
   type Color is (Red, Green, Blue);

   package Color_IO is new Ada.Text_IO.Enumeration_IO (Color);
   use Color_IO;

   C : Color := Red;
begin
   for I in Color'Range loop
      C := I;
      Put ("[");
      Put (C, Width => 5, Set => Lower_Case);
      Put_Line ("]");
   end loop;
end Enum_Demo;

The brackets are not part of the formatting; they make the field boundaries visible. red and blue are padded to five characters, while green fills the field exactly. Without the brackets the trailing spaces would be invisible in a terminal.

$ gnatmake -gnat2022 -gnatwa enum_demo.adb
$ ./enum_demo
[red  ]
[green]
[blue ]

An applied example: an aligned table

A table combines the three generic packages. Each product has a fixed-width name, an integer count, and a real price. The name is already padded to eight characters by its type, the count is written in six columns, and the price uses three digits before the point and two after it.

table_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Table_Demo is
   package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
   package Float_IO is new Ada.Text_IO.Float_IO (Float);
   use Int_IO, Float_IO;

   type Product is record
      Name  : String (1 .. 8);
      Count : Integer;
      Price : Float;
   end record;

   Items : constant array (1 .. 3) of Product :=
     [(Name => "hammer  ", Count => 3, Price => 12.5),
      (Name => "nail    ", Count => 250, Price => 0.05),
      (Name => "screw   ", Count => 40, Price => 0.12)];
begin
   Put_Line ("name     count price");
   for I in Items'Range loop
      Put (Items (I).Name);
      Put (Items (I).Count, Width => 6);
      Put (Items (I).Price, Fore => 3, Aft => 2, Exp => 0);
      New_Line;
   end loop;
end Table_Demo;

Every data line is exactly 20 characters wide: 8 for the name, 6 for the count, and 6 for the price, whose three digits before the point plus the point and two decimals leave room for the leading space. The header uses the same boundaries, so name starts in column 1, count ends in column 14, and price ends in column 20.

$ gnatmake -gnat2022 -gnatwa table_demo.adb
$ ./table_demo
name     count price
hammer       3 12.50
nail       250  0.05
screw       40  0.12

Next steps

The next article reads data instead of writing it: lines from the standard input, integers, and real numbers, with Get_Line, Get, and End_Of_File. The formatting introduced here then serves to print the results.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have written text with Put, Put_Line, and New_Line, aligned integers with Integer_IO widths, printed values in base 2, 8, and 16, controlled decimal places and exponent notation with Float_IO, changed the case of enumeration output, and combined all three packages in an aligned table. The strings article explains the fixed-width names and slices used in the table.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.