作者involution ( )
標題Re: [C語言] #define的值可以直接這樣用嗎
時間2024-05-16 17:10:03
※ 引述《Wardyal (Wardyal)》之銘言:
: #define PATH "/data/file.txt"
: system("fsync "PATH);
: 有人知道嗎
: 我剛剛看到這種寫法
: 是真的可以這樣喔
第一次看到這種寫法還是在要在 printf 印 int32_t 的時候
因為 int32_t 在不同的環境背後可能是不同型別
所以要可移植的話要寫成
int32_t x = 123;
printf("%"PRId32, x);
在我機器上的 inttypes.h 可以找到
#define PRId32 "d"
感覺規定相鄰的 string literal 會直接接起來就是為了 macro
不過實在挺醜的 = =
--
※ 發信站: 批踢踢實業坊(ptt-website.tw), 來自: 203.77.61.242 (臺灣)
※ 文章網址: https://ptt-website.tw/Marginalman/M.1715850608.A.528
推 Wardyal: 所以我這寫法可以嗎 剛剛code review被打槍 05/16 17:10
→ Wardyal: 叫我用snprintf或直接寫絕對路徑 05/16 17:10
→ yam276: 不要用define 除非跨平台 05/16 17:11
→ Wardyal: 真假 我看一堆就是不會動的參數我們都直接定義define 05/16 17:12