Long integers not long enough

This is a discussion on Long integers not long enough within the C Programming forums, part of the General Programming Boards category; Ok, this is really basic, but I'm not sure why this doesn't work: Code: long a = 1134903170; long b ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    Long integers not long enough

    Ok, this is really basic, but I'm not sure why this doesn't work:
    Code:
    long a = 1134903170;
    long b = 1836311903;
    printf("Stort tall: %li\n", a+b);
    And I'm not sure if the problem is with the %li in the printf or with the resulting long getting too big for the long type?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    1134903170 + 1836311903 = 2971215073. Assuming you're on a typical 32-bit system, LONG_MAX will likely be 2147483647, which means you're overflowing the data type.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Hm. Yes, since Mint/Ubuntu 64-bit isn't very stable, I'm running a 32-bit system. How to get around this? Use hex? :-)
    Last edited by cnewbie1; 10-28-2010 at 06:56 AM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by cnewbie1 View Post
    Hm. Yes, since Mint/Ubuntu 64-bit isn't very stable, I'm running a 32-bit system. How to get around this? Use hex? :-)
    If you need integers to be a specific width, don't use the standard types. Instead, #include <stdint.h> and use (in your case) int64_t or uint64_t.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How to get around this? Use hex? :-)
    Using hexadecimal doesn't change the length of your data type. If you're using a compiler that supports some form of the long long type, that would be your best bet. Otherwise, consider an arbitrary length math library like GMP.

    >Instead, #include <stdint.h> and use (in your case) int64_t or uint64_t.
    This assumes C99, which still isn't widely adopted enough to justify the assumption.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Ah, I've learned something today too :-) (int64_t is a template, isn't it?)

    However, how do I specify an int64_t integer in printf?

    EDIT: Nevermind. I found it out: printf("%lli...

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You can try
    Code:
    long long a = 1134903170;
    unsigned long long b = 1836311903;
    These types comes with C99, but newer MSVC compilers (not C99) know also know these types.

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    I need to ask this question again:

    I'm trying to assign the value 600851475143 to a variable, but even the int64_t cannot handle that. What do I use here?

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Use a double and accept some loss of precision, or use a bignum library like GMP: The GNU MP Bignum Library

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,622
    Quote Originally Posted by cnewbie1 View Post
    I need to ask this question again:

    I'm trying to assign the value 600851475143 to a variable, but even the int64_t cannot handle that. What do I use here?
    An 64bit integer can surely handle that value! Are you sure you arenot doing something wrong? ( Unless your compiler doesnot support 64bit )
    Devoted my life to programming...

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,584
    Quote Originally Posted by cnewbie1
    I'm trying to assign the value 600851475143 to a variable, but even the int64_t cannot handle that. What do I use here?
    You could try 600851475143L or 600851475143UL to see if it makes any difference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The highest number for a signed 64bit intiger is 9223372036854775807
    Your value is 600851475143, it should fit.

    I'm wondering how you're assigning it...

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by laserlight View Post
    You could try 600851475143L or 600851475143UL to see if it makes any difference.
    Don't you mean 600851475143LL or 600851475143ULL?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,584
    Oh yes, I do. The LL in my own username betrayed me.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    I'm using gcc on a Linux 32-bit system (32-bit because 64-bit simply isn'g stable enough, unfortunately).

    When I use this line:
    Code:
    int64_t longnumer = 600851475143;
    gcc gives this warning:
    Code:
    warning: integer constant is too large for ‘long’ type
    Adding LL seems to work, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. long long int with g++/MinGW
    By Standback in forum C++ Programming
    Replies: 3
    Last Post: 08-03-2009, 12:36 AM
  2. Long Integers
    By kabuatama in forum C Programming
    Replies: 24
    Last Post: 01-28-2006, 12:21 PM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  4. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM

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