繁体   English   中英

为什么“unsigned int64_t”在C中出错?

[英]Why “unsigned int64_t” gives an error in C?

为什么以下程序出错?

#include <stdio.h>

int main() 
{
    unsigned int64_t i = 12;
    printf("%lld\n", i);
    return 0;
}

错误:

 In function 'main':
5:19: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'i'
  unsigned int64_t i = 12;
                   ^
5:19: error: 'i' undeclared (first use in this function)
5:19: note: each undeclared identifier is reported only once for each function it appears in

但是,如果我删除unsigned关键字,它工作正常。 那么, 为什么unsigned int64_t i给出错误?

您不能在int64_t类型上应用unsigned修饰符。 它只适用于charshortintlonglong long

您可能想要使用uint64_t ,它是int64_t的无符号对应物。

另请注意int64_t等。 在标题stdint.h中定义,如果要使用这些类型,则应包括该标题。

int64_t不是一些内置类型。 尝试添加#include <stdint.h>来定义这些类型; 然后使用uint64_t ,这意味着你想要的东西。 心连心

int64_t是一个typedef名称 N1570§7.20.1.1p1:

type_f name int N _t指定一个有符号整数类型,其宽度为N ,没有填充位和二进制补码表示。 因此, int8_t表示这样的带符号整数类型,其宽度恰好为8位。

标准列出了§6.7.2p2中合法的组合:

  • 烧焦
  • 签名的char
  • 无符号字符
  • short,signed short,short int或signed short int
  • unsigned short或unsigned short int
  • int,signed或signed int
  • unsigned或unsigned int
  • long,signed long,long int或signed long int
  • unsigned long,或unsigned long int
  • long long,signed long long,long long int,或signed long long int
  • unsigned long long,或unsigned long long int

...

  • typedef名称

与问题无关的类型已从列表中删除。

请注意如何将typedef名称与unsigned混合。


要使用无符号64位类型,您需要:

  • 使用不带unsigned说明符的uint64_t (注意前导u )。

     uint64_t i = 12; 
  • 包括定义uint64_t stdint.h (或inttypes.h )。

  • 要打印uint64_t您需要包含inttypes.h并使用PRIu64

     printf("%" PRIu64 "\\n", i); 

    您也可以转换为unsigned long long ,即64位或更多。 但是,当它不是绝对必要时,最好避免施放,所以你应该更喜欢PRIu64方法。

     printf("%llu\\n", (unsigned long long)i); 

int64_t typedef是这样的:

typedef signed long long int int64_t;

所以, unsigned int64_t i; 变成这样的东西:

unsigned signed long long int i;  

这显然是一个编译器错误。

因此,使用int64_t而不是unsigned int64_t

另外,在程序中添加#include <stdint.h>头文件。

暂无
暂无

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

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