打印unsigned long long int

来源:互联网 发布:新网互联域名代理登录 编辑:程序博客网 时间:2024/04/30 13:34

转自: http://blog.csdn.net/sunlylorn/article/details/7610770


如果你想使用inttypes.h中的int32_tint64_t,uint64_t等数据类型,如何对其进行printf是需要仔细考虑的。

在32位平台和64位平台对uint64_t的定义是不同的:

32位平台: typedef unsigned long long int  uint64_t;
64位平台: typedef unsigned long int   uint64_t;

因此,在这两种平台里printf 一个 uint64_t的变量时就会遇到问题,是选择%llu 还是 %lu就成为了一个问题。
这时候,我们就需要使用inttypes.h中定义的配套宏进行正确的printf。
[cpp] view plain copy
 print?
  1. #define __STDC_FORMAT_MACROS    //如果是C++,还需要包括这个宏  
  2. #include <inttypes.h>   // now PRIu64 will work  
  3. #include <stdio.h>  
  4.   
  5.   
  6. int main()  
  7. {  
  8.     uint64_t x;  
  9.     uint32_t y;  
  10.   
  11.   
  12.     printf("x: %"PRId64", y: %"PRId32"\n", x, y);  
  13.       
  14.     return 0;  
  15. }  

原创粉丝点击