LoginSignup
26
13

More than 5 years have passed since last update.

printf() > size_t型の書式指定子 > %zu

Last updated at Posted at 2015-04-09

SOのやり取りを見ている中で、「Don't print size_t with the %d format specifier, it invokes undefined behavior」という記載があった。 知らなかった。

元々の質問では以下のような部分で%zuでなく%dを使っていたのだろう。

printf("oneInt=%zu\n",sizeof(struct oneInt));

また、%zuでなく%uの場合の問題としては、64bit machineでコンパイル時に警告がでるとのこと
SO: How to print size_t variable portably?

32bit, 64bitで共通のコードとする場合は%zuを使っておく方が無難であるようだ。

長さ修飾子zはC99から導入されたようだ Wikipedia

26
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
13