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.

  1. With Gnat.IO; use Gnat.IO;

    • Imports the Gnat.IO package for input/output operations.
    • The use Gnat.IO; allows direct use of functions like Put and Get without prefixing them.
  2. procedure gnatiochar is

    • Defines the procedure gnatiochar, which serves as the main program.
  3. C : Character;

    • Declares a character variable C to store the user input.
  4. begin

    • Marks the beginning of the executable part of the program.
  5. Put ("Enter a single character: ");

    • Prints a message prompting the user to enter a character.
  6. Get (C);

    • Reads a single character from user input and stores it in C.
  7. Put ("You entered: ");

    • Displays the message "You entered: ".
  8. Put (C);

    • Prints the character entered by the user.
  9. Get (C); (Extra Get 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.
  10. New_Line;

    • Moves the cursor to the next line for better formatting.
  11. 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