简体   繁体   English

如何在С中读取或写入64位数字?

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

Can I take 64 bit numbers input in C with %lld?我可以使用 %lld 在 C 中输入 64 位数字吗?

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.如果您的编译器和标准库支持,您可以使用%lld说明符通过scanf()读取long long的整数,但不能保证它是 64 位的。 ( 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) long long至少可以存储-9223372036854775807-(2**63 - 1) )和+92233720368547758072**63 - 1 )(包括两者)之间的数字,因此应该至少为 64 位)

You can use int64_t type and "%" SCNd64 format specifier from inttypes.h if it is supported in your environment.如果您的环境支持,您可以使用inttypes.h中的int64_t类型和"%" SCNd64格式说明符。 int64_t is a signed integer type with exactly 64bit. int64_t是一个有符号的 integer 类型,正好是 64 位。 You can use uint64_t type and "%" SCNu64 format specifier for unsigned 64-bit integer.您可以将uint64_t类型和"%" SCNu64格式说明符用于无符号 64 位 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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