今天学校 C 语言老师教到字串转数字,我本来写先判别正负号的程式,
可是我同学没有判别正负结果结果也正确,真的很奇怪。以下是程式码
#include <stdio.h>
#include <stdlib.h>
int atoi(char *s){
int n = 0;//, t = 0;
//if(*s == '-'){t = 1;s++;}
while(*s)n = 10*n + (*s++) - '0';
return n;
//return (t ? n : -n);
}
void main(){
printf("%d\n", atoi("-123456789"));
system("pause");
}