-2

I made a program to print an ASCII table up to a certain range (like ASCII from 0 to 104).

#include <stdio.h>

int main(void)
{
    int n;
    printf("\n\nthis program will print list of ASCII values for your computer enter the upper limit for the range - ");
    scanf("%d", &n);

    printf("\nDECIMAL\tCHARACTER");
    for (int i = 0; i <= n; i++)
    {
        printf("\n  %d  |  %c", i, i);
        // printf("\n-------------");
        printf("_");   //when i include this "undescore" it causes problems, the code stops at 27th ASCII character and gets stuck.
    }

    return 0;
}

The 27 decimal corresponds to ASCII SYSTEM CODE - [ESCAPE].

None of the following works, and the code stops at the 27th ASCII character:

printf("__"); //double or multiple underscore does not work.

printf("\n_"); //this doesnt work either.

printf("//_"); //somehow this works.

If I remove that printf("_"); part then the code does not stop at 27 and runs smoothly, the code only stops when I use the underscore (_) character.

If I use a different character (-, *, etc), it runs smoothly.

What can the problem be? Could it be something related to macros?

Edit- when I replaced 'printf("_");' with 'printf("n");' thinking it would perform the same as '\n' some weird language popped up and the language of the terminal changed. when '_' is replaced by 'n'

LearnerK
  • 1
  • 2
  • 2
    https://en.wikipedia.org/wiki/ANSI_escape_code – Hans Passant Feb 13 '23 at 17:19
  • 3
    Escape is the first character of an escape sequence, the actual result depends on the following character(s). In general, you shouldn't try to print control characters in your table. – Barmar Feb 13 '23 at 17:22
  • 1
    `ESC _` is [APC](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html) which does something weird. Characters with code <32 are control characters and you should not try to print them. – teapot418 Feb 13 '23 at 17:24
  • For the control character (00..1F, and 7F), you could print (suitably-encoded) Unicode Code Point `0x2400 + i` instead. For example, for 27, this gets you "␛". – ikegami Feb 13 '23 at 17:24
  • Suggest changing your loop to `for(int i = 32; i <= n; i++)`. Printing the control characters is not likely to be useful. – Steve Summit Feb 13 '23 at 17:27
  • 2
    `#include `, then in the `printf()` replace the second `i` with `isprint(i) ? i : ' '`. – John Bayko Feb 13 '23 at 17:34
  • 3
    Why are you trying to print non-printable characters? – n. m. could be an AI Feb 13 '23 at 17:58
  • it was not that i wanted to print "non printable characters" i was just tinkering around, and something really weird happened when I changed the 'printf("_");' with 'printf("n");' (i thought esc + n was the same as \n and would do the same) instead the language of terminal changed to some other non english text, iam updating the original post with the snapshot. – LearnerK Feb 14 '23 at 20:57

1 Answers1

0

Your terminal support ANSI escape sequences which means it does something with ESC _: Application Program Command. In general, if your printing it, you probably want to make sure it shows up so I am using an alternative format here for non-printable characters:

#include <ctype.h>
#include <stdio.h>

int main(void) {
    printf("\n\nthis program will print list of ASCII values for your computer enter the upper limit for the range - ");
    int n;
    scanf("%d", &n);

    printf("\nDECIMAL\tCHARACTER");
    for (int i = 0; i <= n; i++) {
        if(isprint(i))
            printf("\n  %d  |  %c", i, i);
        else
            printf("\n  %d  | 0x%x", i, i);
        printf("_");
    }
}

Here is a sample output:

 this program will print list of ASCII values for your computer enter the upper limit for the range - 38

DECIMAL CHARACTER
  0  | 0x0_
  1  | 0x1_
  2  | 0x2_
  3  | 0x3_
  4  | 0x4_
  5  | 0x5_
  6  | 0x6_
  7  | 0x7_
  8  | 0x8_
  9  | 0x9_
  10  | 0xa_
  11  | 0xb_
  12  | 0xc_
  13  | 0xd_
  14  | 0xe_
  15  | 0xf_
  16  | 0x10_
  17  | 0x11_
  18  | 0x12_
  19  | 0x13_
  20  | 0x14_
  21  | 0x15_
  22  | 0x16_
  23  | 0x17_
  24  | 0x18_
  25  | 0x19_
  26  | 0x1a_
  27  | 0x1b_
  28  | 0x1c_
  29  | 0x1d_
  30  | 0x1e_
  31  | 0x1f_
  32  |   _
  33  |  !_
  34  |  "_
  35  |  #_
  36  |  $_
  37  |  %_
Allan Wind
  • 23,068
  • 5
  • 28
  • 38