1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ARMのCコンパイラではcharが符号無しだった❣(x86は符号有り)

Last updated at Posted at 2021-06-12

追記:M1 MacのX codeのcharは符号有り

# include <stdio.h>
# include <limits.h>

int main(void)
{
  char c = -1;
  printf("%d\n", c);
  printf("(CHAR_MAX == SCHAR_MAX) = %d\n", CHAR_MAX == SCHAR_MAX);
  return 0;
}

x86 アーキテクチャで実行すると

-1
(CHAR_MAX == SCHAR_MAX) = 1

となりますが、 AArch64 (ARM 64bit) では

255
(CHAR_MAX == SCHAR_MAX) = 0

となります。char と書いたときに符号有りになるか符号無しになるかでこの違いが生じます。これは未定義動作ではないので -fsanitize=undefined で捕まえられないし、 -Wall -Wextra を付けてもコンパイラは教えてくれません…😭

確認は gcc 10.2 と LLVM CLang 11.0 で行いました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?