C99标准具有整数类型,字节大小类似于int64_t.我使用以下代码:
#include <stdio.h>
#include <stdint.h>
int64_t my_int = 999999999999999999;
printf("This is my_int: %I64d\n", my_int);
Run Code Online (Sandbox Code Playgroud)
我得到这个编译器警告:
warning: format ‘%I64d’ expects type ‘int’, but argument 2 has type ‘int64_t’
Run Code Online (Sandbox Code Playgroud)
我尝试过:
printf("This is my_int: %lld\n", my_int); // long long decimal
Run Code Online (Sandbox Code Playgroud)
但我得到同样的警告.我正在使用这个编译器:
~/dev/c$ cc -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5664~89/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)
Run Code Online (Sandbox Code Playgroud)
我应该使用哪种格式打印my_int变量而不发出警告?
根据这个答案,我尝试打印一个uint64_t,但它给了我一个错误:
错误:在'PRIu64'之前预期``)'
以下是显示我想要做的最小代码:
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <cstdio>
class X {
X() {
uint64_t foo = 0;
printf("%07" PRIu64 ": ", foo);
}
};
int main() {}
Run Code Online (Sandbox Code Playgroud)
这个最小的代码编译,但我的实际代码没有.但是,我尝试X::X()在我的实际代码中使用完全相同的2行,这不起作用.
我应该寻找什么来进一步调试?我的实际代码也是#include其他标题.这会导致问题吗?包含标题的顺序是否重要?
编辑
PRIu64在我的机器上定义如下:
# if __WORDSIZE == 64
# define __PRI64_PREFIX "l"
# define __PRIPTR_PREFIX "l"
# else
# define __PRI64_PREFIX "ll"
# define __PRIPTR_PREFIX
# endif
# define PRIu64 __PRI64_PREFIX "u"
Run Code Online (Sandbox Code Playgroud) 可能重复:
如何printf uint64_t?
为什么在我的64位Mac上(我使用的是Clang)uint64_t类型是unsigned long long在64位Ubuntu上的uint64_t类型是unsigned long什么?
这使我很难接到我的printf调用,在两种环境下都不给出编译器警告(或者甚至工作).
我可以尝试使用宏来尽量选择正确的字符串(#define LU无论是%llu或%lu,并在这个过程uglifying的printf串位),但在Mac上我有一个64位字长(所以_LP64会被定义UINTPTR_MAX != 0xffffffff),但它仍然long long用于64位int类型.
// printf macro switch (for the uint64_t's)
#if UINTPTR_MAX == 0xffffffff
// 32-bit
# define LU "%llu"
#else
// assume 64-bit
// special case for OS X because it is strange
// should actually check also for __MACH__
# ifdef __APPLE__
# define …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何printf uint64_t?
我想u_int64_t用C 打印.我想知道格式说明符吗?我是C用户,我想做一个printf().
我想在数组中存储指向文件行号的指针,之后我想从磁盘中检索指定的行.我无法直接存储指向行号的指针,因为当我读回文件时,内存位置会发生变化.所以,我正在存储文件开头的偏移量.为了存储偏移,我使用"uint_64t".但是,由于我的文件大小为200GB,因此"uint_64t"无法表示所有偏移量.
我有以下问题:
除了存储偏移量之外,还有其他一些方法可以存储指向存储在磁盘上的文件的指针.
我可以使用其他一些数据结构(除了uint64_t).
使用C ++系列中的函数来打印类型值std::uint64_t(来自<cstdint>)的格式说明符是什么std::printf()?
C99有PRIu64(from <inttypes.h>),但对我来说还不是完全清楚PRIu64有效的C ++ 11,尽管我可以找到暗示。
没有PRIu64,据我所知,没有单一格式说明符可在所有情况下使用:
std::uint64_t将定义为unsigned long long,格式说明符将为%llu。std::uint64_t将被定义为unsigned long,格式说明符将为%lu。%llu在两种情况下都可以使用。我试图学习如何以十进制和十六进制显示指针值.下面你可以看到我创建一个值并尝试使用指针打印出值和值的位置.
请使代码正常工作,以便以十进制和十六进制打印出值
double val= 1;
printf("The value of val : %f\n",val);
double *ptr;
ptr= &val;
printf("dereference *ptr= %f\n", *ptr);
//Display the location of val with and without pointer use in decimal and hex
//decimal
printf("location of val in decimal with ptr is: %p\n",(void *) ptr);
printf("location of val in decimal without a pointer is: %p\n",(void *) &val );
//hexadecimal THIS IS NOT WORKING
printf("location of val in hex with ptr is: %#x\n", (void *) ptr);
printf("location of val in hex …Run Code Online (Sandbox Code Playgroud) 为什么这段代码不起作用?
#include <stdio.h>
main()
{
UINT64_t ram = 90;
printf("%d""\n", ram);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
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
Run Code Online (Sandbox Code Playgroud)