相关疑难解决方法(0)

如何printf uint64_t?失败:"格式中的虚假尾随'%'

我写了一个非常简单的printf uint64_t测试代码:

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

int main()
{
  uint64_t ui64 = 90;
  printf("test uint64_t : %" PRIu64 "\n", ui64);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我使用ubuntu 11.10(64位)和gcc版本4.6.1进行编译,但失败了:

main.cpp: In function ‘int main()’:
main.cpp:9:30: error: expected ‘)’ before ‘PRIu64’
main.cpp:9:47: warning: spurious trailing ‘%’ in format [-Wformat]
Run Code Online (Sandbox Code Playgroud)

c c++

132
推荐指数
1
解决办法
10万
查看次数

sprintf很有趣

我对sprintf很困惑,这是一个与不同平台有趣的问题.代码:

int main () 
{
    char sql[1024];
    uint32_t app_id = 32;
    uint64_t task_id = 64;
    sprintf(sql, "%u, %u", task_id, app_id);
    printf ("%s\n", sql);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

结果在控制台(MSVC2010调试/释放):64,0

但是,同样的代码在控制台(CentOS64 gcc4.4.6):64,32

任何人都会帮助我,tks!

- - - - - - -更新 - - - - - - - - - - - - -

多谢你们.我读过这篇文章:sprintf for unsigned _int64

实际上,PRIu64"inttypes.h"定义中:I64u在Windows上不支持.所以我可以这样写:

sprintf(sql, "%I64u, %I32u", task_id, app_id);
Run Code Online (Sandbox Code Playgroud)

c linux windows printf

5
推荐指数
1
解决办法
5310
查看次数

标签 统计

c ×2

c++ ×1

linux ×1

printf ×1

windows ×1