0

hi i am interested in those chars which are representable by ascii table. for that reason i am doing the following:

       int t(char c) { return (int) c; }
       ...
       if(!(t(d)>255)) { dostuff(); }

so i am interested in only ascii table representable chars, which i assume after conversion to int should be less than 256, am i right? thanks!

James McNellis
  • 348,265
  • 75
  • 913
  • 977
klm
  • 3
  • 1
  • 3

5 Answers5

5

Usually (not always) a char is 8-bits so all chars would typically have a value of less than 256. So your test would always succeed.

Also, ASCII only goes up to 127, not 255. The characters after that are not standard ASCII, and can vary depending on code pages.

If you are dealing with international characters you should probably use wide characters instead of char.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • +1 for mentioning `char`s can have more than 8 bits. You can use the CHAR_BIT macro to find out the exact value. – Andreas Bonini Jan 22 '10 at 17:50
  • ...but this is wrong (I missed it when I first read the post): "Usually a char is 8-bits so all chars would typically have a value of less than 256. So your test would always succeed." -- Whether a `char` is `signed` or `unsigned` is implementation dependent, so even if 8 bits a char could hold the number `250`, for example – Andreas Bonini Jan 22 '10 at 17:52
  • @Andreas I had never heard of BITS_PER_CHAR (not saying it doesn't exist, google finds enough uses). The standard uses CHAR_BIT as far as I remember. – Pascal Cuoq Jan 22 '10 at 17:53
  • 1
    @Andreas: negative numbers are still less than 256, so the test would still succeed. Whilst the point about signed / unsigned is relevant, I'm not sure that your conclusion is correct. – Mark Byers Jan 22 '10 at 18:11
  • Actually, a char is *always* 8-bit, at least in C99. See this post : http://stackoverflow.com/questions/881894/is-char-guaranteed-to-be-exactly-8-bit-long-in-c/4839654#4839654 to view why (the standard is abundantly quoted). – Norswap May 18 '11 at 17:12
5

Use the library:

#include <ctype.h>

...
if (isascii(d)) { dostuff(); }
Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
1

Two caveats:

  1. The C standard does not decide if char is by default signed or unsigned. If your compiler treated char as signed by default the cast to int could result in negative values instead of the values from 128 to 255 (and this is assuming that your chars are 8-bit, too). Perhaps it's better to use unsigned char if you want to be sure this range will be converted the way you expect.

  2. Technically ASCII is from 0 to 127, everything above is some kind of extension.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
0

char is an integral type in C. You can do the check directly:

char c;
/* assign to c */
if (c >= 0 && c <= 127) {
    /* in ASCII range */
}

I am assuming you don't want to use isascii() (it's not in the C standard, although it is POSIX).

Also, you can check if CHAR_MAX is equal to 127. If it is, you don't need the comparison with 127, since c will not exceed it by definition. Similarly, if CHAR_MIN is 0, then you don't need the comparison with 0. Both CHAR_MIN and CHAR_MAX are defined in limits.h.

I think you're thinking about an integer value overflowing a char, and therefore convert it to an int. But, that doesn't help with overflow since the damage has already been done.

Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
-1

Size of char is always 1 byte (as per standard). For all practical matters this means that a char var cannot have a value bigger than 255. (though there are systems, where a byte has more than 8 bits and thus a char value can be bigger, but these are rare nowadays)

Additional caveat is that if char is not defined as signed or unsigned, so it can be in the -128 to 127 range or the 0 to 255 range. (assuming 8 bits per byte, of course :-))

Meanwhile, the ASCII table is 7-bit, which means it covers the range of 0 to 127. So if you are interested in only ASCII symbols, you can just check if the value of your char var is in that range. No need to cast for the comparison.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
  • `sizeof(char)` is always one, but a char can be more than 8 bits. The standard only specifies it has to be at least 8. – Pascal Cuoq Jan 22 '10 at 17:48
  • -1: sizeof(char) is guaranteed to be 1 byte, this is true. But 1 byte can have more (but not less) than 8 bits. For example some architectures have 32 bits per byte, and therefore an `unsigned char` can hold numbers up to `4294967295`. – Andreas Bonini Jan 22 '10 at 17:49
  • sizeof(char) == 1 means that the size of a char is 1. It doesn't say what 1 is. 1 could be 8 bits or 32 bits. – Richard Pennington Jan 22 '10 at 17:49