是否可以在不损失64位计算机精度的情况下为int64_t分配long long returne值?

Nan*_*ish 0 c c++

我已经实现了以下代码:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<cstdlib>
#include<sys/types.h>
main()
{
    int64_t i64value1 = 0;
    int64_t i64value2 = 0;
    long long llvalue = 0;
    const char *s = "10811535359";
    i64value1 = atoll(s);
    llvalue = atoll(s);
    i64value2 = llvalue;
    printf("s : [%s]\n",s);
    printf("i64value1 : [%d]\n",i64value1);
    printf("llvalue : [%lld]\n",llvalue);
    printf("i64value2 : [%d]\n",i64value2);
}
Run Code Online (Sandbox Code Playgroud)

上述进展的输出是:

s : [10811535359]
i64value1 : [-2073366529]
llvalue : [10811535359]
i64value2 : [-2073366529]
Run Code Online (Sandbox Code Playgroud)

使用的编译器是:

 gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)
Run Code Online (Sandbox Code Playgroud)

操作系统是x86_64 GNU/Linux 2.6.18-194

由于long long是带符号的64位整数,并且对于所有意图和目的,与int64_t类型相同,逻辑上int64_tlong long应该是等效类型.有些地方提到使用int64_t而不是long long.但是当我看到stdint.h时,它告诉我为什么我看到上面的行为:

# if __WORDSIZE == 64 
typedef long int  int64_t; 
# else 
__extension__ 
typedef long long int  int64_t; 
# endif 
Run Code Online (Sandbox Code Playgroud)

在64位编译中,int64_tlong int,而不是long long int.

我的问题是,是否有解决方法/解决方案将长long返回值分配给int64_t而不会丢失64位机器的精度?

提前致谢

jil*_*les 6

损失不会发生在转换中,而是发生在打印中:

printf("i64value1 : [%d]\n",i64value1);
Run Code Online (Sandbox Code Playgroud)

int64_t访问该参数就好像它是一个int.这是未定义的行为,但通常低32位是符号扩展的.

正确的编译器警告(例如gcc -Wformat)应该抱怨这一点.