简体   繁体   中英

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

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

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

Thank you

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

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