unsigned char and -1

Discussion in 'C Programming' started by Alex Vinokur, Oct 25, 2005.

  1. Alex Vinokur

    Alex Vinokur Guest

    I came across the following piece of code:

    #define ERROR -1
    #define STATUS0 0
    #define STATUS1 1
    #define STATUS2 2

    unsigned char foo()
    {
    if (/* condition-A */) return ERROR; /* is that safe? */
    if (/* condition-B */) return STATUS0;
    if (/* condition-C */) return STATUS1;
    return STATUS2;
    }

    Is '-1 returned as unsigned char' safe in the given context?


    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Oct 25, 2005
    #1
    1. Advertising

  2. Alex Vinokur wrote:
    > I came across the following piece of code:
    >
    > #define ERROR -1
    > #define STATUS0 0
    > #define STATUS1 1
    > #define STATUS2 2
    >
    > unsigned char foo()
    > {
    > if (/* condition-A */) return ERROR; /* is that safe? */
    > if (/* condition-B */) return STATUS0;
    > if (/* condition-C */) return STATUS1;
    > return STATUS2;
    > }
    >
    > Is '-1 returned as unsigned char' safe in the given context?


    In the given (restricted) context, yes, it returns UCHAR_MAX.
    [Any value not in the range of an unsigned type is converted
    modulo 1+UTYPE_MAX.]

    However, in broader contexts, it's not so safe, e.g. ...

    if (foo() == ERROR)

    This condition will likely fail even though foo returns ERROR.
    This is because the UCHAR_MAX will likely be promoted to a
    int, and -1 is not UCHAR_MAX.

    --
    Peter
    Peter Nilsson, Oct 25, 2005
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

    "Peter Nilsson" <> wrote in message news:...
    > Alex Vinokur wrote:
    > > I came across the following piece of code:
    > >
    > > #define ERROR -1
    > > #define STATUS0 0
    > > #define STATUS1 1
    > > #define STATUS2 2
    > >
    > > unsigned char foo()
    > > {
    > > if (/* condition-A */) return ERROR; /* is that safe? */
    > > if (/* condition-B */) return STATUS0;
    > > if (/* condition-C */) return STATUS1;
    > > return STATUS2;
    > > }
    > >
    > > Is '-1 returned as unsigned char' safe in the given context?

    >
    > In the given (restricted) context, yes, it returns UCHAR_MAX.
    > [Any value not in the range of an unsigned type is converted
    > modulo 1+UTYPE_MAX.]
    >
    > However, in broader contexts, it's not so safe, e.g. ...
    >
    > if (foo() == ERROR)


    And in this context?

    unsigned char ch1, ch2;
    ch1 = foo();
    /* Stuff-1 */
    if (ch1 == ch2)
    {
    /* Stuff-2 */
    }

    >
    > This condition will likely fail even though foo returns ERROR.
    > This is because the UCHAR_MAX will likely be promoted to a
    > int, and -1 is not UCHAR_MAX.

    [snip]


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Oct 25, 2005
    #3
  4. Alex Vinokur

    Guest

    Alex Vinokur wrote:
    > And in this context?
    >
    > unsigned char ch1, ch2;
    > ch1 = foo();
    > /* Stuff-1 */
    > if (ch1 == ch2)
    > {
    > /* Stuff-2 */
    > }


    and in this context ch2 is undefined :p
    , Oct 25, 2005
    #4
  5. Alex Vinokur

    Alex Vinokur Guest

    <> wrote in message news:...
    >
    > Alex Vinokur wrote:
    > > And in this context?
    > >
    > > unsigned char ch1, ch2;
    > > ch1 = foo();
    > > /* Stuff-1 */
    > > if (ch1 == ch2)
    > > {
    > > /* Stuff-2 */
    > > }

    >
    > and in this context ch2 is undefined :p
    >


    Stuff-1 can change ch2.


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Oct 25, 2005
    #5
  6. Alex Vinokur wrote:
    > I came across the following piece of code:
    >
    > #define ERROR -1
    > #define STATUS0 0
    > #define STATUS1 1
    > #define STATUS2 2
    >
    > unsigned char foo()
    > {
    > if (/* condition-A */) return ERROR; /* is that safe? */

    [...]
    > Is '-1 returned as unsigned char' safe in the given context?


    [comp.lang.c answer]
    Yes, it is safe in the sense that UCHAR_MAX will be returned.
    No, it is not safe, because 'ERROR' invades the namespace reserved to
    the implementation.

    As a general rule, crossposting to comp.lang.c and comp.lang.c++ is a
    poor idea. Not only are these different languages, but when the syntax
    is the same in each language, the semantics may differ, and when the
    syntax and semantics agree, the accepted best practice may differ.
    Martin Ambuhl, Oct 25, 2005
    #6
  7. Alex Vinokur

    Guest

    Alex Vinokur wrote:
    > > > unsigned char ch1, ch2;
    > > > ch1 = foo();
    > > > /* Stuff-1 */
    > > > if (ch1 == ch2)
    > > > {
    > > > /* Stuff-2 */
    > > > }

    > >
    > > and in this context ch2 is undefined :p
    > >

    >
    > Stuff-1 can change ch2.


    A comment can change variable? Now that's something new :p''
    , Oct 25, 2005
    #7
  8. Martin Ambuhl wrote:
    > Alex Vinokur wrote:
    > > I came across the following piece of code:
    > >
    > > #define ERROR -1
    > > #define STATUS0 0
    > > #define STATUS1 1
    > > #define STATUS2 2
    > >
    > > unsigned char foo()
    > > {
    > > if (/* condition-A */) return ERROR; /* is that safe? */

    > [...]
    > > Is '-1 returned as unsigned char' safe in the given context?

    >
    > [comp.lang.c answer]
    > Yes, it is safe in the sense that UCHAR_MAX will be returned.
    > No, it is not safe, because 'ERROR' invades the namespace reserved
    > to the implementation.


    True, although EXXXX identifiers are only reserved if the code
    includes <errno.h>. That said, it is best to avoid defining such
    identifiers at all, in case the code ever gets included by other
    code that might use <errno.h>.

    --
    Peter
    Peter Nilsson, Oct 26, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    571
    Jack Klein
    May 9, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
  3. Ioannis Vranos
    Replies:
    11
    Views:
    750
    Ioannis Vranos
    Mar 28, 2008
  4. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    604
    Ben Bacarisse
    Mar 29, 2008
  5. Alex Vinokur
    Replies:
    9
    Views:
    774
    James Kanze
    Oct 13, 2008
Loading...

Share This Page