C言語 文字列から数値変換の基本と実践
C言語では、atoi()関数を使用して、文字列を整数に変換することができます。例を示します。
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345";
int num = atoi(str);
printf("The converted number is: %d\n", num);
return 0;
}
実行結果は:
The converted number is: 12345
また、sscanf()関数を使用して文字を数字に変換することもできます。例を以下に示す:
#include <stdio.h>
int main() {
char str[] = "67890";
int num;
sscanf(str, "%d", &num);
printf("The converted number is: %d\n", num);
return 0;
}
結果は実行されました。
The converted number is: 67890