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

This is a discussion on Trying to resyrict entry and running total to 32 bit int, but get warning within the C Programming forums, part of the General Programming Boards category; Total newbie question. I'm trying to restrict a user entry to a 32 bit integer. I keep getting the following ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 05:59 PM
  2. Replies: 9
    Last Post: 05-28-2010, 10:11 AM
  3. Replies: 13
    Last Post: 12-09-2008, 10: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, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21