#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define N 50
long my_atol(const char *nptr)
{
int c;
long total = 0;
int sign;
while(isspace((int)(unsigned char)*nptr))//跳过前面的空格
++nptr;
c = (int)(unsigned char)*nptr++;
sign = c;
if(c == '-' || c == '+')
c = (int)(unsigned char) *nptr++;
while(isdigit(c))
{
total = 10 * total + c- '0';
c = (int)(unsigned char)*nptr++;
}
if(sign == '-')
return -total;
else
return total;
}
int main(void)
{
char str[N];
printf("Enter a string:\n");
fgets(str,N,stdin);
printf("%d\n",my_atol(str));
return 0;
}
atol函数简单实现
最新推荐文章于 2020-07-23 21:45:20 发布