comparison is always false due to limited range of data type

Discussion in 'C Programming' started by Dave, Feb 26, 2005.

  1. Dave

    Dave Guest

    hello...
    I wrote a marco for saturation.
    #define clip(x) (char)(x)<0?0:((x)>255?255:(x));
    and use this marco in the program like this...
    char tmp=(char)clip((unsigned_int_16)(tmp1+tmp2));

    which unsigned_int_16 will cast the (tmp1+tmp2) to 16 bit
    unsigned int.

    Each time when I compile this program, the gcc warning me the
    "comparison is always false due to limited range of data type"

    Where leads this problem?
    Please tell me! Thanx~~~

    Dave.
    Dave, Feb 26, 2005
    #1
    1. Advertising

  2. Dave

    Ben Pfaff Guest

    "Dave" <> writes:

    > I wrote a marco for saturation.
    > #define clip(x) (char)(x)<0?0:((x)>255?255:(x));


    [...]

    > Each time when I compile this program, the gcc warning me the
    > "comparison is always false due to limited range of data type"


    Presumably `char' is unsigned, so the comparison (char)(x) < 0
    is always false.

    By the way, you seem to be over-fond of casts. You might want to
    consider which casts are actually necessary and get rid of the
    others.
    --
    "For those who want to translate C to Pascal, it may be that a lobotomy
    serves your needs better." --M. Ambuhl

    "Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt
    Ben Pfaff, Feb 26, 2005
    #2
    1. Advertising

  3. Dave

    Randy Howard Guest

    In article <cvomil$t0m$>,
    says...
    > hello...
    > I wrote a marco for saturation.
    > #define clip(x) (char)(x)<0?0:((x)>255?255:(x));
    > and use this marco in the program like this...
    > char tmp=(char)clip((unsigned_int_16)(tmp1+tmp2));
    >
    > which unsigned_int_16 will cast the (tmp1+tmp2) to 16 bit
    > unsigned int.
    >
    > Each time when I compile this program, the gcc warning me the
    > "comparison is always false due to limited range of data type"


    What do you think x < 0 does on an unsigned data type?


    --
    Randy Howard (2reply remove FOOBAR)
    "Making it hard to do stupid things often makes it hard
    to do smart ones too." -- Andrew Koenig
    Randy Howard, Feb 26, 2005
    #3
    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. John Ratliff

    comparison is always false

    John Ratliff, Feb 1, 2007, in forum: C++
    Replies:
    8
    Views:
    387
    Old Wolf
    Feb 4, 2007
  2. Fresh
    Replies:
    2
    Views:
    620
    Bo Persson
    Apr 22, 2008
  3. Replies:
    6
    Views:
    1,346
    Peter Nilsson
    Oct 26, 2009
  4. Iñaki Baz Castillo
    Replies:
    9
    Views:
    183
    Igor Pirnovar
    Feb 26, 2009
  5. Nachy
    Replies:
    11
    Views:
    560
    Kaz Kylheku
    Mar 14, 2012
Loading...

Share This Page