Switch to full style
C++ code examples
Post a reply

Print the ASCII Set

Thu Nov 13, 2008 2:21 pm

Printing the ASCII codes to the screen.
cpp code
/*
* This program prints out a table of the ascii character values.
*/
#include <stdio.h>
#include <ctype.h>

int main()
{
int code; /* Ascii value. */

for(code = 0; code < 128; ++code) {
/* Print the code and the character itself. */
printf("%3d ", code);
if(isspace(code))
printf(" ");
else if(isprint(code))
printf("%c", code);
else
printf(" ");

/* If any of the interesting categories match, print it. */
if(code == '\n')
printf(" (newline, \\n)\n");
else if(code == ' ')
printf(" (space)\n");
else if(code == '\t')
printf(" (tab, \\t)\n");
else if(isspace(code))
printf(" (blank character)\n");
else if(iscntrl(code))
printf(" (control character)\n");
else if(isupper(code))
printf(" (capitol of %c)\n", tolower(code));
else if(islower(code))
printf(" (lower case of %c)\n", toupper(code));
else if(isdigit(code))
printf(" (digit)\n", toupper(code));
else
putchar('\n');
}
}


In the first printf, the construct %3d is the same as %d, except that printf is instructed to use at least three spaces to represent the number, padding with spaces at the left if necessary. (Use %-3d if you want the padding on the right.) This just keeps the output nicely aligned.

Notice how the integer value code is printed both with %d to get the integer value, and with %c to get the character itself. This works equally well if the variable is declared as char: it can be printed with either %d or %c.



Post a reply
  Related Posts  to : Print the ASCII Set
 few examples of c using ascii     -  
 ascii codes using of it in c     -  
 Unicode, ASCII, UTF-16, and UTF-8 characters in java     -  
 How to save in a file the ASCII Characters? Like "☺"     -  
 Help for Print using php     -  
 print clock using JavaScript     -  
 Difference between PHP echo() and PHP print()?     -  
 Print all MySQL status value     -  
 print element in 2d matrix     -  
 How to print a webcam picture in Jsp     -  

Topic Tags

C++ Variables, C++ Basics