原文链接:http://www.juzicode.com/cppe-rror-dev-c-scanf-return-error-char/
错误提示:
scanf()函数输入字符后,得到的字符不正确,源码:
//juzicode.com / vx:桔子code
//dev-c++5.9.2
#include "stdio.h"
int main(void)
{
int height;
char sex;
scanf("%c %d", &sex, &height);
printf("\n性别:%c,身高:%d\n", sex, height);
return 0;
}
运行结果:

错误原因:
1、sex变量为char型,但是输入的是汉字“男”是宽字节字符,实际是字符数组,仅仅使用用char型变量接收汉字导致转换错误。
解决方法:
1、如果用char型变量表示性别,只能输入单个字符表示性别,代码不变化,输入需要使用单字符:

2、使用字符数组char sex[10]接收输入值:
//juzicode.com / vx:桔子code
//dev-c++5.9.2
#include "stdio.h"
int main(void)
{
int height;
char sex[10];//使用字符数组
scanf("%s %d", &sex, &height); //改用%s
printf("\n性别:%s,身高:%d\n", sex, height);//改用%s
return 0;
}

关注微信公众号:“桔子code”,欢迎后台留言撩我。
