【问题标题】:Why "unsigned int64_t" gives an error in C?为什么“unsigned int64_t”在 C 中给出错误?
【发布时间】:2026-02-21 11:30:01
【问题描述】:

为什么下面的程序会报错?

#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 会报错?

【问题讨论】:

  • 你需要#include &lt;stdint.h&gt;。您还应该使用uint64_t 而不是unsigned int64_t
  • 一旦你包含了正确的头文件,为什么不使用uint64_t类型呢?
  • @PaulR 得到同样的错误。
  • @rsp:你解决了两个问题吗?

标签: c gcc unsigned int64


【解决方案1】:

您不能将unsigned 修饰符应用于int64_t 类型。它仅适用于 charshortintlonglong long

您可能想使用uint64_t,它是int64_t 的未签名对应项。

还要注意int64_t 等人。在标头 stdint.h 中定义,如果您想使用这些类型,则应包括在内。

【讨论】:

    【解决方案2】:

    int64_t 不是一些内置类型。尝试添加#include &lt;stdint.h&gt; 来定义此类类型;然后使用uint64_t,这意味着您似乎打算这样做。小时

    【讨论】:

      【解决方案3】:

      int64_ttypedef 类似于:

      typedef signed long long int int64_t;
      

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

      unsigned signed long long int i;  
      

      这显然是编译器错误。

      因此,请使用 int64_t 而不是 unsigned int64_t

      另外,在你的程序中添加#include &lt;stdint.h&gt; 头文件。

      【讨论】:

        【解决方案4】:

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

        typedef 名称 intN_t 指定宽度为 N 的有符号整数类型,无填充 位和二进制补码表示。因此,int8_t 表示这样一个有符号的 整数类型,宽度正好为 8 位。

        标准在 §6.7.2 p2 中列出了哪些组合是合法的:

        • 字符
        • 签名字符
        • 无符号字符
        • short、signed short、short int 或 signed short int
        • unsigned short 或 unsigned short int
        • int、signed 或 signed int
        • 无符号或无符号整数
        • long、signed long、long int 或 signed long int
        • unsigned long 或 unsigned long int
        • long long、有符号 long long、long long int 或 有符号的 long long int
        • unsigned long long 或 unsigned long long int

        ...

        • 类型定义名称

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

        请注意,您不能将 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);
          

        【讨论】:

        • printf("%llu\n", (unsigned long long)i); - inttypes.h 是必需的标头,因此它将存在。否则删除编译器,它可能有其他错误。
        最近更新 更多