LoginSignup
1
1

More than 5 years have passed since last update.

エスケープシーケンスのよくわからない挙動

Posted at

見事にはまった。"\x02A" こんな書き方はしてはいけない。

main.c
#include <stdio.h>
#include <string.h>

int main(void) {
  char s1[] = {0x02, 'A', '\0'};
  char s2[] = "\x02A";
  printf("strcmp(s1, s2) = %d", strcmp(s1, s2));
  return 0;
}

勘のいい人は STX, 'A', ... , ETX なんてシリアル通信データがあるのだろうと思うのだろうけど。その通りです。コンパイル時に警告はあるそうですが、私の環境は出ませんでした。

ちなみに裏技は "\x02" "A" と書いて回避します。

参考: エスケープシーケンス\xhh[2012.08.31]

1
1
1

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