How to read or write 64-bit numbers in С?

1k Views Asked by At

Can I take 64 bit numbers input in C with %lld?

If not how to? Please give an example when you answer.

Thank you

1

There are 1 best solutions below

2
On

You can read integers of long long with %lld specifier via scanf() if your compiler and standard library supports that, but it is not guaranteed to be 64bit. (long long can store at least numbers between -9223372036854775807 (-(2**63 - 1)) and +9223372036854775807 (2**63 - 1) (both inclusive), so it should be at least 64bit)

You can use int64_t type and "%" SCNd64 format specifier from inttypes.h if it is supported in your environment. int64_t is a signed integer type with exactly 64bit. You can use uint64_t type and "%" SCNu64 format specifier for unsigned 64-bit integer.

Example:

#include <stdio.h>
#include <inttypes.h>

int main(void) {
    int64_t num;
    if (scanf("%" SCNd64, &num) != 1) { /* read 64-bit number */
        puts("read error");
        return 1;
    }
    printf("%" PRId64 "\n", num); /* print the number read */
    return 0;
}