トップページ >  C >  文字列を数値(long型)に変換する
初版2003/11/27: 最終更新日2003/11/27
文字列を数値(long型)に変換する
目次
文字列を数値(long型)に変換する
atolの例
文字列を数値(long型)に変換する
文字列を数値(long型)に変換するには atol を使用します。
atolは第一引数で与えられた文字列(数字の列)を数値(long型)に変換しますが、文字列の途中に a などの数値ではない文字が入っていた場合、その文字の前の文字列までを数値に変換します。
stdlib.hをincludeする必要があります。

atolの例
#include <stdio.h>
#include <stdlib.h>

int main(void){

  char a[]="1000000";
  char b[]="430000a33";
  long x,y;

  x=atol(a);
  y=atol(b);

  printf("x=%ld\n",x);
  printf("y=%ld\n",y);

  return 0;
}