A Handy ASCII Table – Solution

My ASCII table program had several iterations. It’s easy to get carried away, but it’s also easy to be too skimpy on the information. This month’s Exercise is based on my current ASCII program, which has evolved over the years.

Coding this challenge doesn’t require a lot of advanced C programming knowledge. Still, having a handy ASCII table is a necessary thing if you want to confirm code values or explore interesting ASCII tricks.

Central to my solution is a for loop, though it goes only from 0 through 32 — one stick of an ASCII table. The loop contains four printf() statements, one for each stick.

The first printf() statement doesn’t output character values directly, as the control codes would foul the display. The second through fourth printf() statements increase the value output by 32 each time: +32, +64, and finally +96.

2021_08-Exercise.c

#include <stdio.h>

int main()
{
    int x;

    printf("Dec  Oct  Hex  C   \
 Dec  Oct  Hex  C  \
 Dec  Oct  Hex  C  \
 Dec  Oct  Hex  C\n");
    for( x=0; x<32; x++ )
    {
        printf("%3d  %3o  %3x  - | ",
                x, x, x
              );
        printf("%3d  %3o  %3x  %c | ",
                x+32, x+32, x+32, x+32
              );
        printf("%3d  %3o  %3x  %c | ",
                x+64, x+64, x+64, x+64
              );
        printf("%3d  %3o  %3x  %c \n",
                x+96, x+96, x+96, x+96
              );
    }

    return(0);
}

The initial printf() statement at Line 7, which outputs the header row, is split between lines. The backslash character extends the line, wrapping it around to the following line, but without an indent. The indent would become part of the string, which I don’t want. Still, be careful to mind the spaces required between the lines to ensure that the headings line up with the columns.

This source code’s program is named ascii and I keep in my personal bin directory so that I can always summon an ASCII table while I’m working.

I hope you concocted a useful ASCII table solution, using whatever methods or approach you deemed best. Remember, multiple solutions are always possible for any programming puzzle.

2 thoughts on “A Handy ASCII Table – Solution

  1. An NCurses version would be nice. You could move around the console, use colour etc. (Your book taught me everything I know on the subject.)

    As an alternative to the +32 etc thing you could test if x % 32 == 0, and if so start a new column, using NCurses to move around of course.

    Another possible enhancement would be to accept a character as a command line argument and output its ASCII code.

    I think I might have mentioned this before in a comment, but could you write a post about Unicode in C? The font used by the Ubuntu & Mint Terminal has very comprehensive and possibly even complete Unicode coverage so would work well without loads of those squares with 0110 in them.

Leave a Reply