Thread: Trying to resyrict entry and running total to 32 bit int, but get warning

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Trying to resyrict entry and running total to 32 bit int, but get warning

    Total newbie question. I'm trying to restrict a user entry to a 32 bit integer. I keep getting the following warning and when I try to use the restriction it won't work:

    [Warning] this decimal constant is unsigned only in ISO C90

    This is my code:
    Code:
    if(testNum == 1 && testLetter == 0)
    {
       if(entry[0] == 45)
       {                          
          for(i=1; i<strlen(entry); i++)
          {
           total = total*10+entry[i]-'0';                       
          }
           total = total*-1;
       }
       else
          for(i=0; i<strlen(entry); i++)
          {
           total = total*10+entry[i]-'0';
          }
     if((total > 2147483647) || (total < -2147483648) || (total+runTotal>2147483647) ||  (total+runTotal < -2147483648))
     {
      printf("Number out of range.");
    }else
      runTotal += total;
      total = 0;      
      printf("\nNumber:%d\n",runTotal);  
    }
    Yes I'm sure there are more efficient ways of doing this, but I'm just trying to learn C and get a feel for type sizes and how to work with them.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    A 32-bit signed integer can NOT hold a value larger than 2147483647. Thus, checking whether a 32-bit signed integer exceeds this value is illogical. It cannot happen. As I said in another post just a day or two ago, this is like asking, "Is this object outside the universe?" The answer is no.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you try to do this
    Code:
    if ( a + b > INT_MAX ) {
      // too late, overflow happened
    }
    Instead, do this
    Code:
    if ( a > INT_MAX - b ) {
      // a+b would overflow, so be careful!
    }
    INT_MAX is in limits.h
    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
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Makes perfect sense! Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-12-2011, 06:59 PM
  2. Replies: 9
    Last Post: 05-28-2010, 10:11 AM
  3. Replies: 13
    Last Post: 12-09-2008, 11:09 AM
  4. Total()
    By janet in forum C Programming
    Replies: 3
    Last Post: 10-03-2002, 12:00 PM
  5. Warning to those running executables posted to the board
    By *ClownPimp* in forum C++ Programming
    Replies: 11
    Last Post: 04-03-2002, 08:48 PM