#include<stdio.h>
#include<ctype.h>
int My_atoi(char *str)//将字符串转化为数字
{
int num=0;
while(*str==' ')
{
str++;
}
while(*str=='-')
{
printf("-");
str++;
}
while(isdigit(*str)) //isdigit()是一个检查参数是否为数字字符的函数
{
num=num*10+(*str-'0');//字符数字-0字符,‘1’-‘0’相当于49-48;如果写成‘\0’或0或0x00的话,就是49-0
str++;
}
return num;
}
int main()
{
char str[] = " 12";
int p = My_atoi(str)/2;//判断转成的是否为数字,若为数字能执行运算
printf("%d\n",My_atoi(str));
printf("%d\n",p);
//C语言中关于0字符
char ch1 = 0x00;//0
char ch2 = 0;//0
char ch3 = '\0';//0
char ch4 = '0';//48,代表的是字符0,上面三个是数字0
printf("%d\n",ch1);
printf("%d\n",ch2);
printf("%d\n",ch3);
printf("%d\n",ch4);
getchar();
return 0;
}
答案:12,6,0,0,0,48