C++ Program to Print ASCII Table (0 – 127)

«
»
This C++ Program which prints the ASCII table from 0 to 127. The program uses a while loop to go through every ASCII character from 0 to 127. ASCII character from 0 to 31 are control characters and are printed specially using an array of character strings, the characters from 32 to 126 are printed by using the character representation of the integer value and the character representation of 127 is printed using the character string “DEL”.

Here is source code of the C++ program which prints the ASCII table from 0 to 127. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Print ASCII table (0 - 127)
  3.  */
  4.  
  5. #include<iostream>
  6. #include<iomanip>
  7. using namespace std;
  8.  
  9. char const* character[] = {"", "", "", "", "", "", "", "",
  10.                             "\\a","\\b","\\t","\\n","\\v","\\f","\\r", "",
  11.                             "", "", "", "", "", "", "", "",
  12.                             "", "", "", "", "", "", "", ""};
  13.  
  14. int main()
  15. {
  16.     char c;
  17.     int row;
  18.     cout << " ASCII Table" << endl << "=============" << endl;
  19.     for(int i = 0; i < 16; i++)
  20.     {
  21.         row = i;
  22.         while (row <= 127) {
  23.             if (row < 32)
  24.                 cout << setfill('0') << setw(2) << setbase(16)
  25.                      << row << " = " << setw(3) << setfill(' ')
  26.                      << character[i] << " | ";
  27.             else if (row >= 32 && row < 127)
  28.             {
  29.                 c = row;
  30.                 cout << setfill('0') << setw(2) << setbase(16)
  31.                      << row << " = " << setw(3) << setfill(' ')
  32.                      << c << " | ";
  33.             }
  34.             else
  35.                 cout << setfill('0') << setw(2) << setbase(16)
  36.                      << row << " = " << setw(3) << setfill(' ')
  37.                      << "DEL" << " | ";
  38.             row = row + 16;
  39.         }
  40.         cout << endl;
  41.     }
  42. } 
  43. cout << "!" << endl;
  44. }

$ g++ main.cpp
$ ./a.out
 ASCII Table
=============
00 =     | 10 =     | 20 =     | 30 =   0 | 40 =   @ | 50 =   P | 60 =   ` | 70 =   p | 
01 =     | 11 =     | 21 =   ! | 31 =   1 | 41 =   A | 51 =   Q | 61 =   a | 71 =   q | 
02 =     | 12 =     | 22 =   " | 32 =   2 | 42 =   B | 52 =   R | 62 =   b | 72 =   r | 
03 =     | 13 =     | 23 =   # | 33 =   3 | 43 =   C | 53 =   S | 63 =   c | 73 =   s | 
04 =     | 14 =     | 24 =   $ | 34 =   4 | 44 =   D | 54 =   T | 64 =   d | 74 =   t | 
05 =     | 15 =     | 25 =   % | 35 =   5 | 45 =   E | 55 =   U | 65 =   e | 75 =   u | 
06 =     | 16 =     | 26 =   & | 36 =   6 | 46 =   F | 56 =   V | 66 =   f | 76 =   v | 
07 =     | 17 =     | 27 =   ' | 37 =   7 | 47 =   G | 57 =   W | 67 =   g | 77 =   w | 
08 =  \a | 18 =  \a | 28 =   ( | 38 =   8 | 48 =   H | 58 =   X | 68 =   h | 78 =   x | 
09 =  \b | 19 =  \b | 29 =   ) | 39 =   9 | 49 =   I | 59 =   Y | 69 =   i | 79 =   y | 
0a =  \t | 1a =  \t | 2a =   * | 3a =   : | 4a =   J | 5a =   Z | 6a =   j | 7a =   z | 
0b =  \n | 1b =  \n | 2b =   + | 3b =   ; | 4b =   K | 5b =   [ | 6b =   k | 7b =   { | 
0c =  \v | 1c =  \v | 2c =   , | 3c =   < | 4c =   L | 5c =   \ | 6c =   l | 7c =   | | 
0d =  \f | 1d =  \f | 2d =   - | 3d =   = | 4d =   M | 5d =   ] | 6d =   m | 7d =   } | 
0e =  \r | 1e =  \r | 2e =   . | 3e =   > | 4e =   N | 5e =   ^ | 6e =   n | 7e =   ~ | 
0f =     | 1f =     | 2f =   / | 3f =   ? | 4f =   O | 5f =   _ | 6f =   o | 7f = DEL |

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.