ZetCode

Ada attributes

last modified November 23, 2026

An attribute is a property of a type or of a value, written with an apostrophe, as in Integer'Image or Word'Length. Attributes are part of the language rather than a library, so they work for every type, including the ones a program declares itself. This article covers the attributes that convert between values and text, describe a string, report a type's size, and compute limits.

Image and Value

'Image turns a value into a String, and 'Value turns a String back into a value of the named type. The two are designed as a round trip: an image produced by 'Image can be read by 'Value without any further parsing. An integer image begins with a space when the value is not negative, so the sign always occupies one character position.

image_value.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Image_Value is
   Count : constant Integer := 42;
   Flag  : constant Boolean := True;
   Text  : constant String := Integer'Image (Count);
   Back  : constant Integer := Integer'Value (Text);
begin
   Put_Line ("text =[" & Text & "]");
   Put_Line ("back =" & Integer'Image (Back));
   Put_Line ("float =" & Float'Image (2.5));
   Put_Line ("boolean =" & Boolean'Image (Flag));
end Image_Value;

The brackets show the leading space in Text, and the second line shows that 'Value accepts it and returns 42. Float'Image uses the default scientific notation with six significant digits, while Boolean'Image writes TRUE without any padding. The image of an integer is therefore not the same as its decimal spelling without blanks: the leading position belongs to the sign.

$ gnatmake -gnat2022 -gnatwa image_value.adb
$ ./image_value
text =[ 42]
back = 42
float = 2.50000E+00
boolean =TRUE

A string that cannot be parsed

'Value accepts only text that forms a valid value of the type. When the string does not, the attribute raises Constraint_Error instead of returning a default, and the message repeats the offending text. The program stops unless the call is protected, which is the subject of the exceptions article later in the series.

bad_value.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Bad_Value is
   N : Integer;
begin
   N := Integer'Value ("forty-two");
   Put_Line ("n =" & Integer'Image (N));
end Bad_Value;

The compiler accepts the program, because the string is a valid String and only its content is wrong. The failure occurs at run time, before the Put_Line call, so no output is produced on the standard output stream. The same rule applies to a real value, a character, or an enumeration literal: the text must be a complete image of a value of the type.

$ gnatmake -gnat2022 -gnatwa bad_value.adb
$ ./bad_value

raised CONSTRAINT_ERROR : bad input for 'Value: "forty-two"

First, Last, Length, and Range

A string carries its bounds with it, so the program does not have to remember the length separately. 'First and 'Last give the lower and upper index, 'Length gives the number of characters, and 'Range gives the whole index range, which can be used directly in a for loop. An Ada string starts at index 1, so these attributes report 1, 8, 8, and 1 .. 8 for the word below.

word_info.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Word_Info is
   Word : constant String := "Ada 2022";
begin
   Put_Line ("length =" & Integer'Image (Word'Length));
   Put_Line ("first =" & Integer'Image (Word'First));
   Put_Line ("last =" & Integer'Image (Word'Last));
   Put ("indices:");
   for I in Word'Range loop
      Put (Integer'Image (I));
   end loop;
   New_Line;
end Word_Info;

The string has eight characters, from index 1 to index 8, and the loop visits every index exactly once. The range stops at 'Last, so a longer or shorter string would produce a different number of iterations without any change to the loop. Reading a character is done with the same index, as Word (I), which the strings article later in the series develops in more detail.

$ gnatmake -gnat2022 -gnatwa word_info.adb
$ ./word_info
length = 8
first = 1
last = 8
indices: 1 2 3 4 5 6 7 8

Size

'Size reports how many bits a type or object occupies in memory. The value depends on the target and on the representation chosen by the compiler, so it is a property to inspect rather than a constant to assume. On this 64-bit GNAT installation an Integer and a Float take 32 bits, a Character takes 8, and a Boolean is packed into a single bit.

sizes.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Sizes is
begin
   Put_Line ("Integer'Size =" & Integer'Image (Integer'Size));
   Put_Line ("Float'Size =" & Integer'Image (Float'Size));
   Put_Line ("Boolean'Size =" & Integer'Image (Boolean'Size));
   Put_Line ("Character'Size =" & Integer'Image (Character'Size));
end Sizes;

The four lines describe the default representation, not a promise that every value is stored in that many bits: a compiler may place a Boolean in a register or in a byte when it is a standalone variable. The attribute is useful for arrays and records, where the size of the component type determines the layout, and for interfacing with hardware and foreign code, where the exact number of bits matters.

$ gnatmake -gnat2022 -gnatwa sizes.adb
$ ./sizes
Integer'Size = 32
Float'Size = 32
Boolean'Size = 1
Character'Size = 8

Min and Max

The attributes 'Min and 'Max return the smaller or larger of two values of a scalar type. Each takes two parameters, so they compose with expressions and avoid a conditional in the middle of a calculation. The type prefix decides which comparison is used, and both arguments must have that type.

limits.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Limits is
   A : constant Integer := 3;
   B : constant Integer := 7;
begin
   Put_Line ("min =" & Integer'Image (Integer'Min (A, B)));
   Put_Line ("max =" & Integer'Image (Integer'Max (A, B)));
   Put_Line ("float min =" & Float'Image (Float'Min (2.5, -1.5)));
end Limits;

The smaller of 3 and 7 is 3, and the larger is 7. The last line compares two real values and returns -1.5. 'Min and 'Max do not change their arguments and do not require the values to be constants; they simply return one of the two, so the result has the same type as the parameters.

$ gnatmake -gnat2022 -gnatwa limits.adb
$ ./limits
min = 3
max = 7
float min =-1.50000E+00

Image of any object in Ada 2022

Older versions of Ada defined 'Image only for scalar types, and the prefix had to be a type name. Ada 2022 allows the prefix to be any object, and the attribute produces a string that represents that object. For a string the result is quoted, which distinguishes it from the text it contains; for a Boolean or an integer the result is the familiar text.

any_image.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Any_Image is
   Word   : constant String := "Ada";
   Flag   : constant Boolean := True;
   Number : constant Integer := 7;
begin
   Put_Line (Word'Image);
   Put_Line (Flag'Image);
   Put_Line (Number'Image);
end Any_Image;

The first line prints "Ada" with the quotation marks included, which distinguishes the string image from the text it contains. The Boolean prints TRUE, and the integer prints with its usual leading space. The object prefix works for constants and variables alike, and it saves writing the type name twice when the type is long or when the object is the only thing of interest.

$ gnatmake -gnat2022 -gnatwa any_image.adb
$ ./any_image
"Ada"
TRUE
 7

Value for enumeration types

An enumeration type has its own 'Value, which accepts the literal names in any case and ignores surrounding spaces. Its 'Image writes the name in upper case, and Ada 2022 lets the object serve as the prefix. Together the two attributes let a program store an enumeration in a text file and read it back without writing a lookup table.

color_value.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Color_Value is
   type Color is (Red, Green, Blue);
   C : Color := Red;
begin
   C := Color'Value ("green");
   Put_Line ("color =" & C'Image);
   Put_Line ("image =" & Color'Image (C));
end Color_Value;

The lower-case text "green" is accepted and produces the literal Green. Both output lines are identical because C'Image and Color'Image (C) are two spellings of the same attribute; the first uses the object, the second the type name. A text that is not one of the three literal names, such as "Purple", would raise Constraint_Error at the assignment.

$ gnatmake -gnat2022 -gnatwa color_value.adb
$ ./color_value
color =GREEN
image =GREEN

Next steps

The scalar types and subtypes article explains the ranges and enumerations that these attributes describe, and operators and expressions uses the comparisons that 'Min and 'Max replace. The next article, conditionals, shows how the Boolean results of those comparisons choose between alternative statements.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have converted values to text with 'Image and back with 'Value, met the failure of an invalid conversion, inspected a string with 'First, 'Last, 'Length, and 'Range, measured type sizes, computed minima and maxima, used the Ada 2022 'Image for any object, and read an enumeration from text. The previous article defines the types these attributes belong to, and the next article uses their results in conditionals.

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.