I have tried scanning long long
integers succesfully; but, I don't know how to scan larger integers than long long
. How would one scan Integers bigger than long long
in c? For instance, long long long
.
-
3There is no `long long long`. To use really big numbers see https://stackoverflow.com/questions/310276/how-to-handle-arbitrarily-large-integers. – sashoalm Feb 28 '15 at 04:33
1 Answers
There is no standard library function which is guaranteed to work with integers wider than long long int
. And there is no portable datatype which is wider than long long int
. (Also, long long int
is not guaranteed to be longer than 64 bits, which might also be the width of long
.)
In other words, if you want to work with very large integers, you'll need a library. These are typically called "bignum libraries", and you'll have little trouble searching for one.
Edit (including the theoretical possibility that the implementation has another integer type, as noted by @chux):
In theory, a C implementation may have integers wider than long long
, in which case the widest such integer types (signed and unsigned) would be called intmax_t
and uintmax_t
. Otherwise, and more commonly, intmax_t
and uintmax_t
are typedefs for long long
and unsigned long long
. You can use intmax_t
variables in scanf
and printf
functions by applying the length modifier j
. For example:
/* Needed for intmax_t */
#include <stdint.h>
#include <stdio.h>
int main() {
intmax_t probably_long_long;
if (1 == scanf("%jd", &probably_long_long))
printf("The number read was %jd\n", probably_long_long);
return 0;
}

- 234,347
- 28
- 237
- 341
-
1"no standard library function which works with integers wider than `long long int`" Hmmm `intmax_t, strtoimax(), strtoumax(), imaxdiv(), imaxabs()` etc. have the potential to be wider. – chux - Reinstate Monica Mar 04 '15 at 03:25
-
@chux: true, if intmax_t is not the same as long long. But I believe that OP really is looking for a bignum library. All the same, edited. – rici Mar 04 '15 at 04:38
-
-
1it's a bit weird that gcc has `__int128` but makes `intmax_t` smaller than that – M.M Mar 04 '15 at 04:55
-
@MattMcNabb: Nothing in my answer denies the existence of potential types wider than long long and narrower than intmax_t. It just doesn't name them. I suppose that `__int128` is not considered an extended integer type because if it were, `intmax_t` would have to be (at least) 128 bits wide, and then the printf/scanf formats mentioned would have to work. But, afaik, they don't. In fact, I don't believe you can even use numeric literals wider than 64 bits... – rici Mar 04 '15 at 05:05