日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++中正确使用PRId64

發(fā)布時(shí)間:2023/12/10 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++中正确使用PRId64 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

nt64_t用來(lái)表示64位整數(shù),在32位系統(tǒng)中是long long int,在64位系統(tǒng)中是long int,所以打印int64_t的格式化方法是:

[cpp]?view plaincopy
  • printf("%ld",?value);?//?64bit?OS??
  • printf("%lld",?value);?//?32bit?OS??
  • 當(dāng)然有跨平臺(tái)的方法:

    [cpp]?view plaincopy
  • #include?<inttypes.h>??
  • printf("%"?PRId64?"\n",?value);??
  • //?相當(dāng)于64位的:??
  • printf("%"?"ld"?"\n",?value);??
  • //?或32位的:??
  • printf("%"?"lld"?"\n",?value);??
  • 其中,printf("abc" "def" “ghi")這樣寫(xiě)多個(gè)字符串是沒(méi)有問(wèn)題的。

    但是,死活都編譯不過(guò),錯(cuò)誤是:error: expected ‘)’ before ‘PRId64’

    找了一下這個(gè)宏的定義,/usr/include/inttypes.h:

    [cpp]?view plaincopy
  • /*?The?ISO?C99?standard?specifies?that?these?macros?must?only?be?
  • ???defined?if?explicitly?requested.??*/??
  • #if?!defined?__cplusplus?||?defined?__STDC_FORMAT_MACROS??
  • ??
  • #?if?__WORDSIZE?==?64??
  • #??define?__PRI64_PREFIX????"l"??
  • #??define?__PRIPTR_PREFIX???"l"??
  • #?else??
  • #??define?__PRI64_PREFIX????"ll"??
  • #??define?__PRIPTR_PREFIX??
  • #?endif??
  • ??
  • /*?Macros?for?printing?format?specifiers.??*/??
  • ??
  • /*?Decimal?notation.??*/??
  • #?define?PRId8??????"d"??
  • #?define?PRId16?????"d"??
  • #?define?PRId32?????"d"??
  • #?define?PRId64?????__PRI64_PREFIX?"d"??
  • 原來(lái)這個(gè)是定義給c用的,C++要用它,就要定義一個(gè)__STDC_FORMAT_MACROS宏顯示打開(kāi)它。

    [cpp]?view plaincopy
  • /*?test_int64.cpp?
  • g++?-D__STDC_FORMAT_MACROS?-o?test_int64?-g?-O0?test_int64.cpp?
  • */??
  • #include?<stdio.h>??
  • #include?<inttypes.h>??
  • ??
  • int?main(int?argc,?char**?argv){??
  • ????int64_t?value?=?0xFFFFFFFFFFFF;??
  • ????printf("int64_t=%"PRId64",?sizeof(int64_t)=%d\n",?value,?sizeof(int64_t));??
  • }??

  • 編譯并執(zhí)行:

    g++ -D__STDC_FORMAT_MACROS -o test_int64 -g -O0 test_int64.cpp

    ./test_int64

    int64_t=281474976710655, sizeof(int64_t)=8

    對(duì)于C++新標(biāo)準(zhǔn)-std=c++0x,還可以使用更好的方式:

    [cpp]?view plaincopy
  • /*?test_int64_1.cpp??
  • g++?-o?test_int64_1?-g?-O0?test_int64_1.cpp?
  • */??
  • #include?<stdio.h>??
  • #include?<cinttypes>??
  • using?namespace?std;??
  • ??
  • int?main(int?argc,?char**?argv){??
  • ????int64_t?value?=?0xFFFFFFFFFFFF;??
  • ????printf("int64_t=%"PRId64",?sizeof(int64_t)=%d\n",?value,?sizeof(int64_t));??
  • }??
  • 不用定義那個(gè)宏了,編譯和執(zhí)行:

    g++ -o test_int64_1 -g -O0 test_int64_1.cpp -std=c++0x

    ./test_int64_1

    int64_t=281474976710655, sizeof(int64_t)=8

    當(dāng)然得指定一個(gè)新的參數(shù):-std=c++0x,否則會(huì)報(bào)錯(cuò)“#error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.”

    若能使用較新的g++編譯,可以使用后者,否則可以用前者直接定義宏。

    總結(jié)

    以上是生活随笔為你收集整理的C++中正确使用PRId64的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。