ADA Program to print a character entered by user
Program
With Gnat.IO; use Gnat.IO;
procedure gnatiochar is
C : Character;
begin
Put ("Enter a single character: ");
Get (c);
Put ("You entered: ");
Put (c);
Get (c);
New_Line;
end;
This program demonstrates basic character input handling in Ada. It reads a single character from the user and prints it.
-
With Gnat.IO; use Gnat.IO;
- Imports the
Gnat.IO
package for input/output operations. - The
use Gnat.IO;
allows direct use of functions likePut
andGet
without prefixing them.
- Imports the
-
procedure gnatiochar is
- Defines the procedure
gnatiochar
, which serves as the main program.
- Defines the procedure
-
C : Character;
- Declares a character variable
C
to store the user input.
- Declares a character variable
-
begin
- Marks the beginning of the executable part of the program.
-
Put ("Enter a single character: ");
- Prints a message prompting the user to enter a character.
-
Get (C);
- Reads a single character from user input and stores it in
C
.
- Reads a single character from user input and stores it in
-
Put ("You entered: ");
- Displays the message
"You entered: "
.
- Displays the message
-
Put (C);
- Prints the character entered by the user.
-
Get (C);
(ExtraGet
Statement)- This is likely unintended or meant to pause execution (waiting for an extra input).
- In some environments, this helps handle newline characters from previous input.
-
New_Line;
- Moves the cursor to the next line for better formatting.
-
end;
- Marks the end of the program.
Key Features:
- Uses
Gnat.IO
for input and output. - Reads a single character and prints it.
- Handles user input efficiently.
Output
$ gnat make gnatiochar.adb
gcc -c gnatiochar.adb
gnatbind -x gnatiochar.ali
gnatlink gnatiochar.ali
$ ./gnatiochar
Enter a single character: s
You entered: s