如何在C中打印一个int64_t类型?

时间:2021-10-23 16:41:43

C99 standard has integer types with bytes size like int64_t. I am using the following code:

C99标准有整数类型,字节大小如int64_t。我使用以下代码:

#include <stdio.h>
#include <stdint.h>
int64_t my_int = 999999999999999999;
printf("This is my_int: %I64d\n", my_int);

and I get this compiler warning:

我得到了这个编译器警告:

warning: format ‘%I64d’ expects type ‘int’, but argument 2 has type ‘int64_t’

I tried with:

我试着:

printf("This is my_int: %lld\n", my_int); // long long decimal

But I get the same warning. I am using this compiler:

但我得到了同样的警告。我使用这个编译器:

~/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)

Which format should I use to print my_int variable without having a warning?

在不发出警告的情况下,我应该使用哪种格式打印my_int变量?

6 个解决方案

#1


310  

For int64_t type:

int64_t类型:

#include <inttypes.h>
int64_t t;
printf("%" PRId64 "\n", t);

for uint64_t type:

uint64_t类型:

#include <inttypes.h>
uint64_t t;
printf("%" PRIu64 "\n", t);

you can also use PRIx64 to print in hexadecimal.

您还可以使用PRIx64来打印十六进制。

cppreference.com has a full listing of available macros for all types including intptr_t (PRIxPTR). There are separate macros for scanf, like SCNd64.

cppreference.com提供了所有类型的可用宏的完整列表,包括intptr_t (PRIxPTR)。scanf有单独的宏,比如SCNd64。


A typical definition of PRIu16 would be "hu", so implicit string-constant concatenation happens at compile time.

PRIu16的一个典型定义是“hu”,所以在编译时就会出现隐式的字符串常量连接。

For your code to be fully portable, you must use PRId32 and so on for printing int32_t, and "%d" or similar for printing int.

为了使您的代码完全可移植,您必须使用PRId32等来打印int32_t,以及“%d”或类似的打印int。

#2


48  

The C99 way is

C99的方法是

#include <inttypes.h>
int64_t my_int = 999999999999999999;
printf("%" PRId64 "\n", my_int);

Or you could cast!

或者你可以把!

printf("%ld", (long)my_int);
printf("%lld", (long long)my_int); /* C89 didn't define `long long` */
printf("%f", (double)my_int);

If you're stuck with a C89 implementation (notably Visual Studio) you can perhaps use an open source <inttypes.h> (and <stdint.h>): http://code.google.com/p/msinttypes/

如果您使用的是C89实现(尤其是Visual Studio),那么您可以使用一个开放源代码 和< stdint.h >):http://code.google.com/p/msinttypes/ 。h>

#3


8  

With C99 the %j length modifier can also be used with the printf family of functions to print values of type int64_t and uint64_t:

在C99中,%j长度修饰符也可以与printf系列函数一起使用,以打印in64_t和uint64_t类型的值:

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

int main(int argc, char *argv[])
{
    int64_t  a = 1LL << 63;
    uint64_t b = 1ULL << 63;

    printf("a=%jd (0x%jx)\n", a, a);
    printf("b=%ju (0x%jx)\n", b, b);

    return 0;
}

Compiling this code with gcc -Wall -pedantic -std=c99 produces no warnings, and the program prints the expected output:

用gcc -Wall -pedantic -std=c99编译这段代码不会产生任何警告,程序会输出预期的输出:

a=-9223372036854775808 (0x8000000000000000)
b=9223372036854775808 (0x8000000000000000)

This is according to printf(3) on my Linux system (the man page specifically says that j is used to indicate a conversion to an intmax_t or uintmax_t; in my stdint.h, both int64_t and intmax_t are typedef'd in exactly the same way, and similarly for uint64_t). I'm not sure if this is perfectly portable to other systems.

这是根据我的Linux系统上的printf(3) (man page特别指出,j是用来指示转换到intmax_t或uintmax_t的;在我stdint。h, int64_t和intmax_t都是用完全相同的方式定义的类型,类似于uint64_t。我不确定这是否适合其他系统。

#4


5  

Coming from the embedded world, where even uclibc is not always available, and code like

来自嵌入式世界,即使uclibc也不总是可用,代码也一样。

uint64_t myval = 0xdeadfacedeadbeef; printf("%llx", myval);

uint64_t myval = 0 xdeadfacedeadbeef;printf(" %拉兹卡”,myval);

is printing you crap or not working at all -- i always use a tiny helper, that allows me to dump properly uint64_t hex:

打印你的垃圾或者根本不工作——我总是使用一个小助手,这让我可以正确地转储uint64_t hex:

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

char* ullx(uint64_t val)
{
    static char buf[34] = { [0 ... 33] = 0 };
    char* out = &buf[33];
    uint64_t hval = val;
    unsigned int hbase = 16;

    do {
        *out = "0123456789abcdef"[hval % hbase];
        --out;
        hval /= hbase;
    } while(hval);

    *out-- = 'x', *out = '0';

    return out;
}

#5


4  

In windows environment, use

在windows环境下,使用

%I64d

in Linux, use

在Linux中,使用

%lld

#6


-1  

//VC6.0 (386 & better)

/ / VC6.0(386 &更好)

    __int64 my_qw_var = 0x1234567890abcdef;

    __int32 v_dw_h;
    __int32 v_dw_l;

    __asm
        {
            mov eax,[dword ptr my_qw_var + 4]   //dwh
            mov [dword ptr v_dw_h],eax

            mov eax,[dword ptr my_qw_var]   //dwl
            mov [dword ptr v_dw_l],eax

        }
        //Oops 0.8 format
    printf("val = 0x%0.8x%0.8x\n", (__int32)v_dw_h, (__int32)v_dw_l);

Regards.

的问候。

#1


310  

For int64_t type:

int64_t类型:

#include <inttypes.h>
int64_t t;
printf("%" PRId64 "\n", t);

for uint64_t type:

uint64_t类型:

#include <inttypes.h>
uint64_t t;
printf("%" PRIu64 "\n", t);

you can also use PRIx64 to print in hexadecimal.

您还可以使用PRIx64来打印十六进制。

cppreference.com has a full listing of available macros for all types including intptr_t (PRIxPTR). There are separate macros for scanf, like SCNd64.

cppreference.com提供了所有类型的可用宏的完整列表,包括intptr_t (PRIxPTR)。scanf有单独的宏,比如SCNd64。


A typical definition of PRIu16 would be "hu", so implicit string-constant concatenation happens at compile time.

PRIu16的一个典型定义是“hu”,所以在编译时就会出现隐式的字符串常量连接。

For your code to be fully portable, you must use PRId32 and so on for printing int32_t, and "%d" or similar for printing int.

为了使您的代码完全可移植,您必须使用PRId32等来打印int32_t,以及“%d”或类似的打印int。

#2


48  

The C99 way is

C99的方法是

#include <inttypes.h>
int64_t my_int = 999999999999999999;
printf("%" PRId64 "\n", my_int);

Or you could cast!

或者你可以把!

printf("%ld", (long)my_int);
printf("%lld", (long long)my_int); /* C89 didn't define `long long` */
printf("%f", (double)my_int);

If you're stuck with a C89 implementation (notably Visual Studio) you can perhaps use an open source <inttypes.h> (and <stdint.h>): http://code.google.com/p/msinttypes/

如果您使用的是C89实现(尤其是Visual Studio),那么您可以使用一个开放源代码 和< stdint.h >):http://code.google.com/p/msinttypes/ 。h>

#3


8  

With C99 the %j length modifier can also be used with the printf family of functions to print values of type int64_t and uint64_t:

在C99中,%j长度修饰符也可以与printf系列函数一起使用,以打印in64_t和uint64_t类型的值:

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

int main(int argc, char *argv[])
{
    int64_t  a = 1LL << 63;
    uint64_t b = 1ULL << 63;

    printf("a=%jd (0x%jx)\n", a, a);
    printf("b=%ju (0x%jx)\n", b, b);

    return 0;
}

Compiling this code with gcc -Wall -pedantic -std=c99 produces no warnings, and the program prints the expected output:

用gcc -Wall -pedantic -std=c99编译这段代码不会产生任何警告,程序会输出预期的输出:

a=-9223372036854775808 (0x8000000000000000)
b=9223372036854775808 (0x8000000000000000)

This is according to printf(3) on my Linux system (the man page specifically says that j is used to indicate a conversion to an intmax_t or uintmax_t; in my stdint.h, both int64_t and intmax_t are typedef'd in exactly the same way, and similarly for uint64_t). I'm not sure if this is perfectly portable to other systems.

这是根据我的Linux系统上的printf(3) (man page特别指出,j是用来指示转换到intmax_t或uintmax_t的;在我stdint。h, int64_t和intmax_t都是用完全相同的方式定义的类型,类似于uint64_t。我不确定这是否适合其他系统。

#4


5  

Coming from the embedded world, where even uclibc is not always available, and code like

来自嵌入式世界,即使uclibc也不总是可用,代码也一样。

uint64_t myval = 0xdeadfacedeadbeef; printf("%llx", myval);

uint64_t myval = 0 xdeadfacedeadbeef;printf(" %拉兹卡”,myval);

is printing you crap or not working at all -- i always use a tiny helper, that allows me to dump properly uint64_t hex:

打印你的垃圾或者根本不工作——我总是使用一个小助手,这让我可以正确地转储uint64_t hex:

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

char* ullx(uint64_t val)
{
    static char buf[34] = { [0 ... 33] = 0 };
    char* out = &buf[33];
    uint64_t hval = val;
    unsigned int hbase = 16;

    do {
        *out = "0123456789abcdef"[hval % hbase];
        --out;
        hval /= hbase;
    } while(hval);

    *out-- = 'x', *out = '0';

    return out;
}

#5


4  

In windows environment, use

在windows环境下,使用

%I64d

in Linux, use

在Linux中,使用

%lld

#6


-1  

//VC6.0 (386 & better)

/ / VC6.0(386 &更好)

    __int64 my_qw_var = 0x1234567890abcdef;

    __int32 v_dw_h;
    __int32 v_dw_l;

    __asm
        {
            mov eax,[dword ptr my_qw_var + 4]   //dwh
            mov [dword ptr v_dw_h],eax

            mov eax,[dword ptr my_qw_var]   //dwl
            mov [dword ptr v_dw_l],eax

        }
        //Oops 0.8 format
    printf("val = 0x%0.8x%0.8x\n", (__int32)v_dw_h, (__int32)v_dw_l);

Regards.

的问候。