Re: Cast within an IF condition

Discussion in 'C++' started by Victor Bazarov, Jul 31, 2003.

  1. "Alan Sung" <> wrote...
    > I have isolated a case which is inconsistent on several compilers and

    wanted
    > to see what the correct behavior is supposed to be.
    >
    > #include <stdio.h>
    > int main(int argc, char* argv[])
    > {
    > unsigned short s = 256;
    > if ((unsigned char) s)
    > printf ("first case TRUE\n");
    > else
    > printf ("first case FALSE\n");
    >
    > unsigned char c = (unsigned char) s;
    > if (c)
    > printf ("second case TRUE\n");
    > else
    > printf ("second case FALSE\n");
    >
    > return 0;
    > }
    >
    > I am trying to test the low order byte.
    > Under MS Visual Studio 6, Solaris Sun Pro 6.2, Linux gcc, and OS/390 V2R10
    > C++ I get FALSE, FALSE
    >
    > Under z/OS V1R4 C++ and AIX Visual Age 5 xlc I get TRUE, FALSE.
    >
    > There's no difference if I replace the C-style casts with static_cast<>
    >
    > Opinions?


    What is std::numeric_limits<unsigned char>::digits on z/OS C++? What is
    it on AIX Visual Age? If they both are 8, the compilers are broken. But
    if they are greater than 8, they are OK.

    The expression in 'if's parentheses should be converted to 'bool'. That
    yields 'true' if it's not 0, and 'false' if it's 0. In your case, the
    first expression (with the cast right there, in the parentheses) should
    create a temporary of unsigned char type and initialise it according to
    the rules of integral conversions (see 4.7). If 'unsigned char' can
    represent values 0..255 (often the case on IBM PC, Mac, other similar
    systems), then (unsigned char)256 is going to be 0. However, if the
    representable range is greater, then 256 is a valid value for unsigned
    char, and static_cast<unsigned char>(256) will be 256, and conversion to
    bool will yield 'true'.

    Victor
     
    Victor Bazarov, Jul 31, 2003
    #1
    1. Advertising

  2. "Victor Bazarov" <> wrote...
    > "Alan Sung" <> wrote...
    > > I have isolated a case which is inconsistent on several compilers and

    > wanted
    > > to see what the correct behavior is supposed to be.
    > >
    > > #include <stdio.h>
    > > int main(int argc, char* argv[])
    > > {
    > > unsigned short s = 256;
    > > if ((unsigned char) s)
    > > printf ("first case TRUE\n");
    > > else
    > > printf ("first case FALSE\n");
    > >
    > > unsigned char c = (unsigned char) s;
    > > if (c)
    > > printf ("second case TRUE\n");
    > > else
    > > printf ("second case FALSE\n");
    > >
    > > return 0;
    > > }
    > >
    > > I am trying to test the low order byte.
    > > Under MS Visual Studio 6, Solaris Sun Pro 6.2, Linux gcc, and OS/390

    V2R10
    > > C++ I get FALSE, FALSE
    > >
    > > Under z/OS V1R4 C++ and AIX Visual Age 5 xlc I get TRUE, FALSE.
    > >
    > > There's no difference if I replace the C-style casts with static_cast<>
    > >
    > > Opinions?

    >
    > What is std::numeric_limits<unsigned char>::digits on z/OS C++? What is
    > it on AIX Visual Age? If they both are 8, the compilers are broken. But
    > if they are greater than 8, they are OK.


    That's where I went wrong. If CHAR_BIT is greater than 8, they would
    display TRUE TRUE, right? So, they must be broken.

    > [...]


    Victor
     
    Victor Bazarov, Jul 31, 2003
    #2
    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. -
    Replies:
    12
    Views:
    696
    Remon van Vliet
    Jun 15, 2005
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,079
    Dan Pop
    Feb 10, 2004
  3. EvilRix
    Replies:
    8
    Views:
    648
    Martin Dickopp
    Feb 14, 2004
  4. Pavel
    Replies:
    7
    Views:
    535
    Pavel
    Sep 19, 2010
  5. Bill W.
    Replies:
    13
    Views:
    294
    Phillip Gawlowski
    May 9, 2011
Loading...

Share This Page