C语言怎样将单个字符转化为整型字符

#include<stdio.h> #include<stdlib.h> #include <string.h> int c[20]; char b[20]; void main() { gets(b); for(int i=0;i<(int)strlen(b);i++) { c[1]=atoi(&b[1]); } printf("%c\n",b[1]); printf("%d\n",c[1]); } 我运行程序输入1234,结果输出 2 234。我想让它输出2 2。要怎样修改程序呢??希望哪路英雄不吝赐教哈!!!

#include<stdio.h>
#include<stdlib.h>
#include
<string.h>
int
c[20];
char
b[20];
int
_tmain(int
argc,
_TCHAR*
argv[])
{
char
temp[2];
gets(b);
temp[0]
=
b[1];
temp[1]
=
'\0';
c[1]=atoi(temp);
printf("%c\n",b[1]);
printf("%d\n",c[1]);
return
0;
}
你用atoi(&b[1]),实际上相当于atoi("234"),返回的结果当然是234,
如果你需要单个字符的转换,就需要自己手动构建个含有单个字符的字符串,temp就是做这事的。
温馨提示:答案为网友推荐,仅供参考
相似回答