0

I am trying to represent a very long number (i.e between 13 to 16 numerical digits) using C . long long does not seem to work as I am always getting an overflow problem.

I would appreciate it if someone can help me with this problem, thank you.

long long number = 123654123654123LL;
printf("%ull", number);
m0skit0
  • 25,268
  • 11
  • 79
  • 127
Sinan
  • 453
  • 2
  • 10
  • 20
  • 3
    You can use a library, see: http://stackoverflow.com/questions/565150/bigint-in-c – Justin Ethier Aug 31 '12 at 15:31
  • 2
    You shouldn't attempt to print a long long as an unsigned long long. – Richard J. Ross III Aug 31 '12 at 15:33
  • `long long` is guaranteed to hold up to at least 2^63-1, which has 19 decimal digits (to those who saw my previous comment -- yep, I can't count). So once you fix your print statement you should be good to go. – Steve Jessop Aug 31 '12 at 15:41
  • Steve, thank you, the print statement was not correct due to a typo mistake, in the program it was written with small 'p' letter. – Sinan Aug 31 '12 at 15:49

1 Answers1

6

The format specifier is incorrect it should be %llu and the type for number should be unsigned long long:

#include<stdio.h>

int main()
{
    unsigned long long number = 123654123654123LL;
    printf("%llu\n", number);

    return 0;
}

Output:

123654123654123

See http://ideone.com/i7pLX.

The format specifier %ull is actually %u followed by two l (ell) characters.

From the section 5.2.4.2.1 Sizes of integer types <limits.h> of the C99 standard the maximum values for long long and unsigned long long are guaranteed to be at least:

LLONG_MAX +9223372036854775807 // 263 − 1
ULLONG_MAX 18446744073709551615 // 264 − 1

so 123654123654123 is comfortably within range.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Thank you, it is a typo mistake, was not intentional. Thank you for noting it. – Sinan Aug 31 '12 at 15:36
  • Thank you, I'm using dev c++, I wrote the same code as suggested but still it overflow... – Sinan Aug 31 '12 at 15:44
  • @Sinan, `#include ` and print out the values of the _Limits of integer types_ macros listed on [this page](http://en.cppreference.com/w/c/types/limits). That will tell you what is supported. On my 32-bit XP machine the `ULLONG_MAX` has a value of `18446744073709551615`. – hmjd Aug 31 '12 at 15:48
  • @Sinan 123654123654123 is too small to overflow any legitimate `long long` or `unsigned long long` type. What exactly happens when you compile and run hmjd's code? – Daniel Fischer Aug 31 '12 at 15:54
  • @hmjd, thank you. I did that and the output was 4294967295 which is smaller than the required number I am trying to print. My code was printf("%llu", ULL_MAX); can you confirm if that is the correct way to do it. Thank you. – Sinan Aug 31 '12 at 15:57
  • @Daniel, thanks. When I wrote hmjd code and run it, the output was this number (201511380). – Sinan Aug 31 '12 at 15:59