difference between short and char

Hi all,

What is the difference between the data types char and short? According to the book, they are both 8 bits and have the exact same range.

Thanks,
Steve

Parents

  • Which book? My C51 compiler manual PDF (version 7.02) says that a short is 16 bits.

    ANSI C requires a short to be at least 16 bits, and a long to be at least 32.

    In any event, if a short were 8 bits wide, it would be the same as a char -- just as in actuality, a "short" is the same as an "int" on this particular compiler.

    Most programmers I know adopted a habit of typedef'ing specific integer sizes as appropriate for their platform, and then using those typedefs instead. Something along the lines of:

    typedef unsigned char U8;
    typedef signed char S8;
    typedef unsigned short U16;
    typedef signed short S16;
    typedef unsigned long U32;
    typedef signed long S32;

    That way you always know what you're dealing with, and don't have as much trauma moving from platform to platform. Of course, there's lots of variation: uint8, UBYTE / UWORD / ULONG, and so on.

    The ISO C99 standard also has a bunch of new integer types in <inttypes.h>, which serve the same purpose, among others. (intptr_t, for instance: an integer guaranteed to be long enough to hold a pointer).

Reply

  • Which book? My C51 compiler manual PDF (version 7.02) says that a short is 16 bits.

    ANSI C requires a short to be at least 16 bits, and a long to be at least 32.

    In any event, if a short were 8 bits wide, it would be the same as a char -- just as in actuality, a "short" is the same as an "int" on this particular compiler.

    Most programmers I know adopted a habit of typedef'ing specific integer sizes as appropriate for their platform, and then using those typedefs instead. Something along the lines of:

    typedef unsigned char U8;
    typedef signed char S8;
    typedef unsigned short U16;
    typedef signed short S16;
    typedef unsigned long U32;
    typedef signed long S32;

    That way you always know what you're dealing with, and don't have as much trauma moving from platform to platform. Of course, there's lots of variation: uint8, UBYTE / UWORD / ULONG, and so on.

    The ISO C99 standard also has a bunch of new integer types in <inttypes.h>, which serve the same purpose, among others. (intptr_t, for instance: an integer guaranteed to be long enough to hold a pointer).

Children
More questions in this forum