unsigned char

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lars Tackmann

    unsigned char

    Does the c standard state that for all c99 implementation char as standard
    is equal unsigned char ??? - or do i have to take this into account when
    writing my program ???.
  • Hallvard B Furuseth

    #2
    Re: unsigned char

    Lars Tackmann wrote:
    [color=blue]
    > Does the c standard state that for all c99 implementation char as standard
    > is equal unsigned char ?[/color]

    Not at all. It's equivalent to either signed char or unsigned char.
    The choice is up to the implementation.

    It's not _equal_ to either, though. It's a distinct type, so
    char *foo;
    unsigned char *bar = foo;
    is an error even if char is equivalent to unsigned char.

    --
    Hallvard

    Comment

    • Irrwahn Grausewitz

      #3
      Re: unsigned char

      Lars Tackmann <roland@diku.dk > wrote:[color=blue]
      >Does the c standard state that for all c99 implementation char as standard
      >is equal unsigned char ??? - or do i have to take this into account when
      >writing my program ???.[/color]

      The implementation has to pick one from {signed|unsigne d} char
      for "plain" char:

      ISO/IEC 9899:1999 6.2.5#15
      The three types char, signed char, and unsigned char are
      collectively called the character types. The implementation
      shall define char to have the same range, representation,
      and behavior as either signed char or unsigned char.

      and

      ISO/IEC 9899:1999 6.3.1.1#3
      The integer promotions preserve value including sign. As
      discussed earlier, whether a "plain" char is treated as
      signed is implementation-defined.


      HTH
      Regards
      --
      Irrwahn Grausewitz (irrwahn33@free net.de)
      welcome to clc : http://www.angelfire.com/ms3/bchambl...me_to_clc.html
      clc faq-list : http://www.eskimo.com/~scs/C-faq/top.html
      acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...acllc-c++.html

      Comment

      • Thomas Stegen

        #4
        Re: unsigned char

        Hallvard B Furuseth wrote:
        [color=blue]
        > Lars Tackmann wrote:
        >
        >[color=green]
        >>Does the c standard state that for all c99 implementation char as standard
        >>is equal unsigned char ?[/color]
        >
        >
        > Not at all. It's equivalent to either signed char or unsigned char.
        > The choice is up to the implementation.
        >
        > It's not _equal_ to either, though. It's a distinct type, so
        > char *foo;
        > unsigned char *bar = foo;
        > is an error even if char is equivalent to unsigned char.
        >[/color]

        Note that pointers to unsigned char can point to any byte in an object.
        So if foo had been properly initialised the above would have been legal.

        But even
        int *p;
        int *p2 = p;

        is an error since it uses the value of an uninitialised variable.

        --
        Thomas.

        Comment

        • Christopher Benson-Manica

          #5
          Re: unsigned char

          Thomas Stegen <tstegen@cis.st rath.ac.uk> spoke thus:
          [color=blue]
          > int *p;
          > int *p2 = p;[/color]

          Is ( p==NULL ) likewise undefined?

          --
          Christopher Benson-Manica | I *should* know what I'm talking about - if I
          ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

          Comment

          • Jeremy Yallop

            #6
            Re: unsigned char

            Christopher Benson-Manica wrote:[color=blue]
            > Thomas Stegen <tstegen@cis.st rath.ac.uk> spoke thus:
            >[color=green]
            >> int *p;
            >> int *p2 = p;[/color]
            >
            > Is ( p==NULL ) likewise undefined?[/color]

            Yes. No expression that uses the value of an uninitialized pointer
            variable has defined behaviour.

            Jeremy.

            Comment

            • Christopher Benson-Manica

              #7
              Re: unsigned char

              Jeremy Yallop <jeremy@jdyallo p.freeserve.co. uk> spoke thus:
              [color=blue]
              > Yes. No expression that uses the value of an uninitialized pointer
              > variable has defined behaviour.[/color]

              Does that hold true for non-pointer variables as well? Such as

              int i;
              if( i != 42 ) {
              printf( "What a tragedy...\n" );
              }

              --
              Christopher Benson-Manica | I *should* know what I'm talking about - if I
              ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

              Comment

              • Jeremy Yallop

                #8
                Re: unsigned char

                Christopher Benson-Manica wrote:[color=blue]
                > Jeremy Yallop <jeremy@jdyallo p.freeserve.co. uk> spoke thus:
                >[color=green]
                >> Yes. No expression that uses the value of an uninitialized pointer
                >> variable has defined behaviour.[/color]
                >
                > Does that hold true for non-pointer variables as well?[/color]

                Not in every case. Since the bit pattern of an uninitialized object
                is indeterminate, whether the behaviour is defined depends on whether
                all

                It does, but the particular type of behaviour - undefined,
                unspecified, etc. - depends on the type of the variable and possibly
                on the implementation. For example, all bit patterns have valid
                unsigned char values, so using an uninitialized unsigned char object
                in an arithmetic expression never invokes undefined behaviour. Some
                such expressions may even have well-defined behaviour; the following
                assertion cannot fail.

                unsigned char c;
                assert(!(c - c));
                [color=blue]
                > Such as
                >
                > int i;
                > if( i != 42 ) {
                > printf( "What a tragedy...\n" );
                > }[/color]

                It's unspecified whether or not this code has undefined behaviour. On
                an implementation where all possible bit patterns represent valid int
                values the behaviour is not undefined, because the standard imposes
                requirements on it: `i' has either the value 42 or some other valid
                value. On an implementation with trap values for int, the code has
                undefined behaviour.

                Jeremy.

                Comment

                • Christopher Benson-Manica

                  #9
                  Re: unsigned char

                  Jeremy Yallop <jeremy@jdyallo p.freeserve.co. uk> spoke thus:
                  [color=blue]
                  > Not in every case. Since the bit pattern of an uninitialized object
                  > is indeterminate, whether the behaviour is defined depends on whether
                  > all[/color]

                  Lose your train of thought? Did Agent Smith pay you an unwelcome
                  visit? ;)
                  [color=blue]
                  > It does, but the particular type of behaviour
                  > (etc.)[/color]

                  Glad to see Agent Smith let you live this time. Seriously, thanks.

                  --
                  Christopher Benson-Manica | I *should* know what I'm talking about - if I
                  ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                  Comment

                  • Hallvard B Furuseth

                    #10
                    Re: unsigned char

                    Thomas Stegen wrote:[color=blue]
                    > Hallvard B Furuseth wrote:
                    >[color=green]
                    >> char *foo;
                    >> unsigned char *bar = foo;
                    >> is an error even if char is equivalent to unsigned char.[/color]
                    >
                    > Note that pointers to unsigned char can point to any byte in an object.
                    > So if foo had been properly initialised the above would have been legal.[/color]

                    No, assignment between different pointer types are not legal.
                    OTOH, using a cast is OK: unsigned char *bar = (unsigned char*) foo;
                    [color=blue]
                    > But even
                    > int *p;
                    > int *p2 = p;
                    >
                    > is an error since it uses the value of an uninitialised variable.[/color]

                    Oops. I should have said 'char *foo = "something" ;'.

                    --
                    Hallvard

                    Comment

                    • Simon Biber

                      #11
                      Re: unsigned char

                      "Thomas Stegen" <tstegen@cis.st rath.ac.uk> wrote:[color=blue]
                      > Hallvard B Furuseth wrote:[color=green]
                      > > It's not _equal_ to either, though. It's a distinct type, so
                      > > char *foo;
                      > > unsigned char *bar = foo;
                      > > is an error even if char is equivalent to unsigned char.[/color]
                      >
                      > Note that pointers to unsigned char can point to any byte in
                      > an object. So if foo had been properly initialised the above
                      > would have been legal.[/color]

                      Well, no, it would still be a constraint violation because of the
                      incompatible types in the initialisation. The type (char *) is
                      never compatible with the type (unsigned char *), and nor is it
                      ever compatible with the type (signed char *).

                      To make it legal you can either use a cast, or an intermediate
                      variable of type (void *) as conversions to and from (void *)
                      do not require casts.

                      Example with cast:
                      char *foo = 0; /* Properly initialise */
                      unsigned char *bar = (unsigned char *)foo; /* Convert with cast */

                      Example without cast:
                      char *foo = 0; /* Properly initialise */
                      void *bar = foo; /* Convert to (void *) */
                      unsigned char *baz = bar; /* Convert from (void *) */
                      [color=blue]
                      > But even
                      > int *p;
                      > int *p2 = p;
                      >
                      > is an error since it uses the value of an uninitialised variable.[/color]

                      Though this is not a contraint violation, it is merely undefined
                      behaviour, so the compiler is not required to detect it and
                      generate a diagnostic message.

                      --
                      Simon.


                      Comment

                      Working...