25

Just curious if u_char is a standard. I've always used it assuming it was defined along with uintX_t types and so on. But am seeing some of our code base transition from u_char to "unsigned char" with a reason "so users don't have to define u_char themselves"..

Jason
  • 2,233
  • 3
  • 24
  • 27

3 Answers3

15

The string u_char does not appear in this draft of the C standard:

http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf

It's not required by POSIX either, as far as I know.

I think it's in BSD (sys/types.h), and Windows (winsock.h). I would not consider either one to be "a standard" - they aren't formal standards, and they certainly aren't part of standard C, but they are clearly defined and documented.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • 2
    Moreover the problem of using these ugly nonstandard types is that you have to be very careful about defining them. Some systems will already have them defined (which is wrong), leading to errors if you define them yourself, but others will not, meaning you can't use the type unless you define it yourself. The safest way is just not to use junk like this, not be lazy, and write out `unsigned char` when you mean it. – R.. GitHub STOP HELPING ICE Jul 01 '11 at 14:57
  • @R.. or use `` for the `uint8_t`, ... types. – Jonathon Reinhart Jun 25 '13 at 04:34
  • 5
    @JonathonReinhart: using `uint8_t` in effect has an in-built assertion that `CHAR_BIT == 8`, since the type is forbidden to exist if `CHAR_BIT > 8`. That's fine if you want an 8 bit type, but if what you mean is "an unsigned integer type occupying one unit of storage in this implementation, whatever that may be", then you should say that: `unsigned char`. – Steve Jessop Jun 25 '13 at 07:42
11

No, u_char is non-standard. If you need to use a standard type that's equivalent to u_char, you can use uint8_t which is part of the C99 standard library (check your specific platforms/compilers for C99-compliance). stdint.h defines this type (along with many other specific integral types). This Wikipedia article contains more information about stdint.h.

Emerick Rogul
  • 6,706
  • 3
  • 32
  • 39
4

It's not present in any older header files (except certain specific areas, like Kerberos and networking headers), and not a built-in type in any compiler I know of.

wallyk
  • 56,922
  • 16
  • 83
  • 148