LoginSignup
0
0

More than 1 year has passed since last update.

【C言語】文字や数字を数値に変換する時の考え方

Last updated at Posted at 2022-09-01

〇数字を数値に変換

(例)数字'4'を数値に変換

コード(省略)
    char c = '4';
    int s = c - '0';
    printf("%d\n",s);
    return 0;
結果
 4

'4' は 数値で表すと 52
'0' は 数値で表すと 48
よって'4'-'0'は52-48で4になる

〇文字を数値に変換(問題あり)

(例)文字'A'を数値に変換

コード(省略)
    char c = 'A';
    int s = c - '0';
    printf("%d\n", s);
    return 0;
結果
 17

'A' は 数値で表すと 65
'0' は 数値で表すと 48
よって'A'-'0'は65-48で17になる

※文字を数値に変換するのは問題がある

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