Strings: Looking at ASCII Code

bumsfeld 1 Tallied Votes 180 Views Share

Commonly strings consist of ASCII characters, some are printable, some are in the backgrounds like the bell, carriage return, linefeed, tab and so forth. This code displays a table of ASCII characters and the corresponding decimal value. As always, you are encouraged to learn here.

/* display the ASCII character table */

#include <stdio.h>

int main()
{
  int k;
  
  for ( k = 14; k < 256; k++ )
  {
    printf ( "%3d = %c ",k, k );
  }
    
  getchar();   /* wait for key */
  return 0;
}

Dani AI

Generated

A few clarifications and a safer approach to the example from , plus a short answer to 's Unicode question.

ASCII is a 7‑bit set: valid code points are 0–127. The printable range is 32–126; 0–31 and 127 are control codes (BEL, CR, LF, TAB, DEL, etc.) that can change terminal behavior or produce no visible glyph. Bytes 128–255 are not part of ASCII — they belong to various single‑byte code pages (ISO‑8859‑1, Windows‑1252, DOS CPs) and will display differently depending on the system encoding and font. Printing raw bytes with %c can therefore be misleading and sometimes cause side effects (a bell or line breaks). Also, isprint() and related ctype functions expect values cast to unsigned char (or EOF), so casting is important.

A safer C approach prints the canonical ASCII range and avoids outputting control characters directly:

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

int main(void) {
    for (int k = 0; k < 128; ++k) {
        if (isprint((unsigned char)k))
            printf("%3d 0x%02X  '%c'\n", k, k, k);
        else
            printf("%3d 0x%02X  [control]\n", k, k);
    }
    return 0;
}

In response to : Unicode is a much larger character set (code points U+0000..U+10FFFF) and must be handled via an encoding (UTF‑8/UTF‑16/UTF‑32). Terminals, fonts, and the C runtime must agree on encoding. A minimal C example using wide characters and the locale:

#include <locale.h>
#include <wchar.h>
#include <stdio.h>

int main(void) {
    setlocale(LC_CTYPE, "");
    wchar_t ch = 0x03A9; /* U+03A9 */
    wprintf(L"U+03A9: %lc\n", ch);
    return 0;
}

That prints correctly only if the environment uses a Unicode-capable locale/terminal. Enumerating all Unicode code points is large and often requires libraries (ICU) or higher‑level languages (Python) that handle encoding, normalization and glyph availability. Troubleshooting tips: check the process locale (LANG), terminal encoding (UTF‑8), and installed fonts; prefer hex and symbolic names when inspecting bytes rather than printing control or non‑supported glyphs.

bofarull 0 Newbie Poster

Hi, can you do the same for UNICODE?

Bofarull

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.