Thread: Strange Error. . .

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    Strange Error. . .

    Code:
    char fgetbyte(FILE *file)
    {
            short retval;
            char num;
    
            num = ahextoc(fgetc(file));
            if (num < 0){
                    return num;
            }
            retval = num;
            retval <<= 4;
            num = ahextoc(fgetc(file));
            if (num < 0){
                    return num;
            }
            retval |= num;
            return retval;
    }
    The bolded above would be line 58.

    Quote Originally Posted by gcc
    reprogram.c:58: warning: comparison is always false due to limited range of data type
    How am I getting this warning? I don't get it. This _IS_ a char not an unsigned char. . .

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    char can be either signed or unsigned. Apparently on your system it is unsigned. Perhaps your ahextoc() function should act like fgetc() and return an int so it can return a negative value on error and a positive value otherwise. Since you're passing the result of fgetc() into the function that should be pretty easy to manage.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try with the appropriate flags
    Quote Originally Posted by gcc manual
    -funsigned-char
    Let the type char be unsigned, like unsigned char.

    Each kind of machine has a default for what char should be. It is either like unsigned char by default or like signed char by default.

    Ideally, a portable program should always use signed char or unsigned char when it depends on the signedness of an object. But many programs have been written to use plain char and expect it to be signed, or expect it to be unsigned, depending on the machines they were written for. This option, and its inverse, let you make such a program work with the opposite default.

    The type char is always a distinct type from each of signed char or unsigned char, even though its behavior is always just like one of those two.
    -fsigned-char
    Let the type char be signed, like signed char.

    Note that this is equivalent to -fno-unsigned-char, which is the negative form of -funsigned-char. Likewise, the option -fno-signed-char is equivalent to -funsigned-char.
    An unqualified char can be signed or unsigned.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Well now that is REALLY annoying. I have two cross compilers for this system. One (gcc 4.2.2) defaults to anything unspecified is signed. This one (gcc 3.2.2) is backwards.

    What a completely freaky compiler. . .

    Thanks Salem!

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    AFAIK, it really isn't necessary to pass around anything smaller than an int, since the compiler will just widen it anyway (at least that's how it I've seen it done).

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    I'm reading an S record file. Each nibble is specified in the file, then I convert that to either the LSB or MSB of a short array. When making assignments to this array, I didn't want the compiler squawking at me for type conversions. <-- I don't know that it would, but that used to be the case with gcc.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Kennedy View Post
    I'm reading an S record file. Each nibble is specified in the file, then I convert that to either the LSB or MSB of a short array. When making assignments to this array, I didn't want the compiler squawking at me for type conversions. <-- I don't know that it would, but that used to be the case with gcc.
    Well, it should for the very fact that Cas pointed out: the standard doesn't specify whether or not a char is to be signed or unsigned (unfortunately!), and so the code is actually undefined behaviour, and isn't guaranteed to work on all systems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM