【问题标题】:How to print UINT64_t in gcc?如何在 gcc 中打印 UINT64_t?
【发布时间】:2015-05-07 05:48:21
【问题描述】:

为什么这段代码不起作用?

#include <stdio.h>
main()
{
    UINT64_t ram = 90;
    printf("%d""\n", ram);
}

我收到以下错误:

In function \u2018main\u2019
error: \u2018UINT64_t\u2019 undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)
error: expected \u2018;\u2019 before \u2018ram\u2019

【问题讨论】:

  • #include &lt;stdint.h&gt;uint64_t
  • 我又遇到了同样的错误。
  • 你换uint64_t了吗?
  • uint64_t,注意大小写。 uint64_t 的正确 printf 说明符来自 &lt;inttypes.h&gt;,并使用文字连接来形成正确的规范,如下所示:printf("%" PRIu64 "\n", val);
  • UINT64_t 不是标准类型或别名。除非您自己 typedef 它,否则您将从其他地方获取它。问问给你代码的人。 标准提供的版本是uint64_t

标签: c function unix


【解决方案1】:

uint64_t 在标准整数类型头文件中定义。即stdint.h。 所以首先在你的程序中包含stdint.h

然后您可以使用格式说明符"%"PRIu64 打印您的值:即

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

你也可以参考这个问题How to print a int64_t type in C

【讨论】:

  • 虽然uint64_t是在&lt;stdint.h&gt;中定义的,但是PRIu64等宏却是在&lt;inttypes.h&gt;中定义的。不同寻常的是,&lt;inttypes.h&gt; 有效地包含了&lt;stdint.h&gt;;没有其他标准 C 标头包含另一个。
【解决方案2】:

完整的工作示例:http://ideone.com/ttjEOB

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>

int main()
{
    uint64_t ram = 90;
    printf("%" PRIu64 "\n", ram);
}

您忘记了一些标题,写错了uint64_t 并且不能将%duint64_t 一起使用

【讨论】:

    【解决方案3】:

    添加:

    #include <inttypes.h>
    

    并使用 PRIu64(像这样在引号之外):

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-11
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2012-07-24
      • 2011-01-12
      • 2017-11-07
      相关资源
      最近更新 更多