-2

I need to hold a value of range 10^20 in C. Heard that the big int in C can hold such big values. How to declare and use the big int in C.

Does anybody know of an easy way to do that? Any help would really be appreciated!

zloster
  • 1,149
  • 11
  • 26
Aswin Raghavan
  • 321
  • 2
  • 14
  • 2
    Well, `10 ^ 20` *does* fit into an ordinary `int` in C. – EOF Feb 13 '16 at 11:39
  • 10^20 is about 2^65, so 128 bit int can handle it, See if `int128_t` is available on your compiler. – user3528438 Feb 13 '16 at 11:41
  • @user3528438: What? `10^20 == 30`. If `INT_MAX < 30`, your implementation is nonconforming. – EOF Feb 13 '16 at 11:43
  • @EOF "^" is not"`^`". – user3528438 Feb 13 '16 at 11:45
  • @user3528438 how to use int128_t – Aswin Raghavan Feb 13 '16 at 11:52
  • Whether `__int128` is available or not depends on your system. What environment do you program for? If it is available, it is just an ordinary type, like `int` or `long int`. – chqrlie Feb 13 '16 at 11:58
  • if `int128_t` is supported, then it should be in `stdint.h`. however according to this http://stackoverflow.com/questions/29638723/why-isnt-there-int128-t there's a high chance that the compiler may provide `__int128` but not `int126_t` so try both. To use `__int128` you don't need to include anything. – user3528438 Feb 13 '16 at 12:02

1 Answers1

1

You can use type unsigned long long, the range is at least 0..18446744073709551615, but that's only 1.8E19, so slightly less than what you need. If you really want to go beyond 64 bits, you can check if your system supports 128 bit integers (as type __int128, __int128_t, int128_t or something similar) or you will need a multi-precision package such as GMP.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Isn't it safe to assume that a compiler that provides `__int128` also provides `int128_t`? – user3528438 Feb 13 '16 at 11:48
  • Whether `__int128` is available or not depends on your system. What environment do you program for? If it is available, it is just an ordinary type, like `int` or `long int`. – chqrlie Feb 13 '16 at 11:58
  • @AswinRaghavan: Did this answer help you? – chqrlie Feb 14 '16 at 10:32
  • @user3528438: I'm afraid it's not. I'm using gcc with the glibc on linux, `__int128_t` is available and functional, but `int128_t` is not and `intmax_t` has 64 bits. The availability of `int128_t` would have wide ranging consequences for the C library and the compiler because `intmax_t` would then be required to have at least 128 bits, the preprocessing arithmetic would be done in 128 bits, etc. – chqrlie Feb 21 '16 at 06:10