Thread: Printable Ascii Characters

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Printable Ascii Characters

    Hi,
    as all of you know there are specified ascii characters which can not be printed depending on your system (depends on if you're on UTF-8, I believe).
    Thing is I have a program in which I print both the number associated to each character on the ascii table and the character itself.
    When printing it though, when the number goes off the rouf (127 plus) my program doesn't print it but I'm guessing it should print the number associated to it.
    It doesn't though, it prints like -56 (always the same numbers though, I keep compiling the file but it always prints the same number for each different number).
    Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    a code snippet would help. if you are printing characters with %c you wouldn't automatically get the number instead of a character for nonprintable characters. you would get whatever your display system interprets that character as. if you are printing with %d then that is why you would see negative numbers for values >127. if you print with %u or %x you would get the unsigned value in the range 0..255 as long as your argument was an unsigned char or cast to unsigned char. if you pass a signed char > 127 to %u or %x it will sign extend and print as ffffffxx. as to why you would see the same number every time in your program, you probably have a bug.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    ASCII does not go beyond 127, so if you're printing an ASCII chart, stop there.

    Apart from that, you forgot to include your code, so it's a bit difficult to figure out what's wrong with it.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The ascii table has 128 values:

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    The ones above 31 are considered "printable" and the ones below 32 "control", altho some of the controls (eg \t \n) might be called printable too.

    There are are various forms of "extended ascii tables" containing another 128 values defined by charsets like ISO-8859-1 (the "Latin-1" set). But these are have generally been superseded by the use of Unicode. If your system is UTF-8 or UTF-16 (most now are), the extended values are used in multi-byte characters, so they do not have a meaning all by themselves.

    The reason you are getting negative values is probably because you are using a signed char. 0-127 is the ascii set, and UTF-8 duplicates these. -128 to -1 are the remaining, non-ascii values (used in the "extended" sets and mulitbyte unicode); for a signed byte, they would be 128-255. But if you post your code, you'll get a more precise answer.
    Last edited by MK27; 03-06-2012 at 11:18 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Escaping non-printable characters in strings
    By HAKUtora in forum C Programming
    Replies: 2
    Last Post: 04-18-2011, 08:44 AM
  2. Counting printable and non-printable chars
    By dmux5 in forum C Programming
    Replies: 7
    Last Post: 07-03-2007, 09:07 PM
  3. ASCII Printable Characters
    By dalek in forum C# Programming
    Replies: 3
    Last Post: 08-11-2003, 04:13 AM
  4. All the ASCII characters
    By Shadow12345 in forum C++ Programming
    Replies: 5
    Last Post: 05-20-2002, 01:20 PM
  5. ascii characters
    By Denethor2000 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2002, 05:26 AM