LoginSignup
0
0

More than 1 year has passed since last update.

[C]配列に漢字の文字列を入れようとしたら表示されなかった話

Last updated at Posted at 2023-04-14

[やりたかったこと]
char str[][3] = {"金", ...};
みたいな宣言して
printf("%s", str[0]); // 金
をやろうとしたら表示されなかった。

[条件]
・vscode
・utf-8

[原因]
utf-8の日本語は3バイトや4バイトで表されるから、宣言した時のメモリの確保した量が足りてなかった。
ちなみにshift-jisは2バイトらしい

[修正]
char str[][100] = ...
メモリの確保量を増やしたら動いた

[反省点]
null文字や表示したい文字が何バイト必要なのかを踏まえて宣言をしよう
あとそもそもメモリの確保量が少なすぎた

0
0
3

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
0
0