用C/C++编写程序,实现将str指向的整数字符串转换为一个整数类型?

只针对-128~127之间的整数,如果不合规定或者超出范围则函数返回0,谢谢大神!!!

#include "stdio.h"
#include <stdlib.h>
int f(char *p){
int x=atoi(p);
return x>=-128 && x<=127 ? x : 0;
}
int main(int argc,char *argv[]){
char str[5];
scanf("%4s",str);
printf("%d\n",f(str));
return 0;
}

运行样例:

温馨提示:答案为网友推荐,仅供参考
相似回答