简体   繁体   中英

How to scanf long long long int in C

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 .

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM