记录学C语言遇到的问题:
#include<stdio.h>
void test02()
{
char ch;
scanf("%s", &ch);
if (ch == 'y')
{
printf("you choice is yes\r\n");
}
else if (ch == 'n')
{
printf("you choice is not\r\n");
}
else
{
}
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
运行结果如下:
运行时检查失败---变量周围的堆栈已损坏。
解决办法:
将上诉代码中scanf中的%s修饰符改为%c
#include<stdio.h>
void test02()
{
char ch;
scanf("%c", &ch);//修改此处
if (ch == 'y')
{
printf("you choice is yes\r\n");
}
else if (ch == 'n')
{
printf("you choice is not\r\n");
}
else
{
}
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
为什么用%s修饰符解释获取的为字符串,会造成变量ch周围的堆栈损毁?
虽然解释输入为字符串,但不还是从第一个不为空字符的字符读到下一个空字符为止,读取一个字符?
有大佬帮帮忙吗(˃ ⌑ ˂ഃ)