Thread: Question about gcc and data types

  1. #1
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121

    Question about gcc and data types

    Hello all,
    I was working on a small ls program for a class assignment and we when trying to check if a character was greater than 127 or outside of the standard ascii bound it gcc gave me the error

    condition is always false due to limited data type

    with the code
    Code:
    usigned char token;
    fread(&token, 1, 1, file_desc);
    if(token > 127) /* this is where it gave me the error */
         break;
    This confused me because I was using an unsigned character type which means that I should have the values 0 - 255 to use instead of using the first bit as the sign bit. No matter what I did it always either failed or told me it would fail. Is there a reason for this?

    Thank you!

    note: I made this work by using a signed variable and checking if it was less than zero because any value that was greater than 128 up to 255 will have to have that bit set to one which would make an 8 bit 2's compliment number negative. But it is still very strange that it would not recognize the unsigned char values greater than 127...
    -- Will you show me how to c++?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    "usigned" is not a valid keyword, what does your REAL code look like?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mramazing View Post
    condition is always false due to limited data type
    I do not get this error even if I use a signed char

    Code:
    char x = 234;
    if (x>123451) puts("okay");
    gcc -Wall test.c
    [root] ./a.out


    Compiles and runs without errors or warnings.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    It appears the compiler is being very nice to you to alert you on the problem with the limited range of your assigned char type. If token is ever assigned anything above 127, it would be undifined behaviour, alter your conditional to 'token <= 127', that would remove that gcc error.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    In other words, always make it explicitly unsigned. And when breaking out of loops, just remember that unsigned checks are generally done near the end of the loop (never as a precondition), naturally.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. c data types help needed for project
    By anush870 in forum C Programming
    Replies: 6
    Last Post: 09-12-2008, 07:33 PM
  4. Very angry: "two or more data types in declaration"
    By quzah in forum C Programming
    Replies: 2
    Last Post: 06-04-2003, 07:16 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

Tags for this Thread