在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<abbr id="27omo"></abbr>

<menu id="27omo"><dl id="27omo"></dl></menu>
    • <label id="27omo"><tt id="27omo"></tt></label>

      博客專欄

      EEPW首頁 > 博客 > C++中正確使用PRId64

      C++中正確使用PRId64

      發(fā)布人:電子禪石 時(shí)間:2022-06-17 來源:工程師 發(fā)布文章
      隨筆 - 74  文章 - 181  評論 - 37  閱讀 - 57萬
      轉(zhuǎn):C++中正確使用PRId64及__STDC_FORMAT_MACROS宏

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

      1. printf("%ld", value); // 64bit OS

      2. printf("%lld", value); // 32bit OS

      當(dāng)然有跨平臺的方法:

      1. #include <inttypes.h>

      2. printf("%" PRId64 "\n", value); 

      3. // 相當(dāng)于64位的:

      4. printf("%" "ld" "\n", value); 

      5. // 或32位的:

      6. printf("%" "lld" "\n", value); 

      其中,printf("abc" "def" “ghi")這樣寫多個(gè)字符串是沒有問題的。

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

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

      復(fù)制代碼
      /* 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"
      復(fù)制代碼

      原來這個(gè)是定義給c用的,C++要用它,就要定義一個(gè)__STDC_FORMAT_MACROS宏顯示打開它。

      編譯并執(zhí)行:

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

      ./test_int64

      int64_t=281474976710655, sizeof(int64_t)=8

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

      復(fù)制代碼
      /* 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));
      }
      復(fù)制代碼

      編譯并執(zhí)行:

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

      ./test_int64

      int64_t=281474976710655, sizeof(int64_t)=8

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

      復(fù)制代碼
      /* 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));
      }
      復(fù)制代碼

      不用定義那個(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,否則會報(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++編譯,可以使用后者,否則可以用前者直接定義宏。


      *博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請聯(lián)系工作人員刪除。



      關(guān)鍵詞: C++

      相關(guān)推薦

      技術(shù)專區(qū)

      關(guān)閉