ZetCode

Ada literals

last modified November 23, 2026

A literal is a value written directly in the source text. Ada has literals for integers, real numbers, characters, and strings, and its numeric literals may contain underscores or be written in another base. This article describes every form, together with the rules that the compiler enforces.

Decimal literals

A decimal integer literal is a run of digits. Underscores may separate groups of digits to make a long number easier to read; they do not change the value. The underscore must sit between digits, so GNAT rejects a trailing underscore and two consecutive underscores.

numbers.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Numbers is
   Population : constant Integer := 1_000_000;
   Distance   : constant Integer := 384_400;
begin
   Put_Line ("population =" & Integer'Image (Population));
   Put_Line ("distance =" & Integer'Image (Distance));
end Numbers;

The first value is one million and the second is three hundred eighty-four thousand four hundred. The underscores are ignored by the compiler, so 1_000_000 and 1000000 are the same literal. The compiler reports digit expected for 1_ and two consecutive underlines not permitted for 1__0.

$ gnatmake -gnat2022 -gnatwa numbers.adb
$ ./numbers
population = 1000000
distance = 384400

Based literals

A based integer literal writes the base first, then the digits between two hash signs, as in 16#FF#. The base may be any value from 2 to 16, and the digits A to F are accepted in either case. Underscores may separate digits inside the number, and the entire literal has the universal integer type.

based.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Based is
   Hex   : constant Integer := 16#FF#;
   Bits  : constant Integer := 2#1010_1010#;
   Octal : constant Integer := 8#17#;
begin
   Put_Line ("hex =" & Integer'Image (Hex));
   Put_Line ("bits =" & Integer'Image (Bits));
   Put_Line ("octal =" & Integer'Image (Octal));
end Based;

16#FF# is 255, 2#1010_1010# is 170, and 8#17# is 15. The base is decimal, so 16# is not a hexadecimal representation of the base. The hash signs are part of the literal; without them, 16FF is rejected with "delimiter required between literal and identifier".

$ gnatmake -gnat2022 -gnatwa based.adb
$ ./based
hex = 255
bits = 170
octal = 15

Based real literals

A based literal may also contain a point, which makes it a real number. An optional exponent after the second hash sign multiplies the value by a power of the base, not by a power of ten. The point must have digits on both sides, and the exponent uses the letter E followed by an optional sign and a decimal integer.

based_real.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Based_Real is
   Half : constant Float := 16#0.8#;
   Big  : constant Float := 16#F.8#E1;
begin
   Put_Line ("half =" & Float'Image (Half));
   Put_Line ("big =" & Float'Image (Big));
end Based_Real;

16#0.8# is 8/16, or 0.5. 16#F.8#E1 is 15.5 multiplied by 16 once, which gives 248.0. The exponent is a power of the base, so the same exponent means a different multiplier in base 2 and in base 16.

$ gnatmake -gnat2022 -gnatwa based_real.adb
$ ./based_real
half = 5.00000E-01
big = 2.48000E+02

Real literals

A decimal real literal contains a point or an exponent. The point must be surrounded by digits: 3.5 is valid, while 3. and .5 are rejected. The exponent is written with E and may carry a sign; the exponent itself is a decimal integer. The value has the universal real type and takes the type of the context.

reals.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Reals is
   Simple : constant Float := 3.5;
   Kilo   : constant Float := 1.5E3;
   Milli  : constant Float := 2.5E-2;
begin
   Put_Line ("simple =" & Float'Image (Simple));
   Put_Line ("kilo =" & Float'Image (Kilo));
   Put_Line ("milli =" & Float'Image (Milli));
end Reals;

1.5E3 is 1500.0 and 2.5E-2 is 0.025. GNAT reports real literal cannot end with point for 3. and numeric literal cannot start with point for .5. A real literal can also be written without an exponent, as in 3.5, and the exponent letter is not case-sensitive.

$ gnatmake -gnat2022 -gnatwa reals.adb
$ ./reals
simple = 3.50000E+00
kilo = 1.50000E+03
milli = 2.50000E-02

Character literals

A character literal is one character between single quotes. The apostrophe itself is written as three quotes in a row, and a space is written as a quoted space. Characters are not strings: 'A' has type Character, while "A" has type String.

chars.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Chars is
   Letter : constant Character := 'A';
   Space  : constant Character := ' ';
   Quote  : constant Character := ''';
begin
   Put ("letter: ");
   Put (Letter);
   New_Line;
   Put ("space: [");
   Put (Space);
   Put_Line ("]");
   Put ("quote: ");
   Put (Quote);
   New_Line;
end Chars;

The call Put (Letter) writes one character, and New_Line ends the line; Put_Line would reject a Character because it expects a string. The three apostrophes in the declaration of Quote are an opening quote, the apostrophe character, and a closing quote. The brackets around the space show that it is present in the output.

$ gnatmake -gnat2022 -gnatwa chars.adb
$ ./chars
letter: A
space: [ ]
quote: '

String literals

A string literal is a sequence of characters between double quotes. A double quote inside the string is written twice, and the empty string is written as two adjacent quotes. There are no backslash escapes: a tab or a control character is produced with a character code, not with \t.

strings.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Strings is
   Simple : constant String := "Ada";
   Quoted : constant String := "say ""hi""";
   Empty  : constant String := "";
begin
   Put_Line (Simple);
   Put_Line (Quoted);
   Put_Line ("[" & Empty & "]");
   Put_Line (Simple & " " & Quoted);
end Strings;

"say ""hi""" contains the text say "hi": each doubled quote becomes one quote. The empty string contributes nothing, so the third line prints a pair of empty brackets. The ampersand joins strings and characters, and the last call builds one line from three operands.

$ gnatmake -gnat2022 -gnatwa strings.adb
$ ./strings
Ada
say "hi"
[]
Ada say "hi"

Next steps

The next article in the series, operators and expressions, shows what can be done with these values, including integer division, remainders, powers, and short-circuit Boolean operators. The variables and constants article explains how literals become initial values.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have written decimal integers with underscores, based integers and reals, decimal reals with exponents, character literals including the apostrophe, and strings with embedded quotes and an empty value. The previous article explains how these values become initial values, and the next article combines them in expressions.

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.