yohhoyの日記

技術的メモをしていきたい日記

2020-06-04から1日間の記事一覧

atoi関数のかしこい実装

C

C標準ライブラリ Muslのatoi関数実装 では、符号付き整数オーバーフロー回避のため負数範囲で10進数値を減算してゆき最後に符号反転を行っている。 int atoi(const char *s) { int n=0, neg=0; while (isspace(*s)) s++; switch (*s) { case '-': neg=1; cas…