|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- // talkback.c -- 演示与用户交互
- #include<stdio.h>
- #include<string.h> //提供strlen()函数的原型
- #define DENSITY 62.4 //人体密度(单位:磅/立方英尺)
- int main()
- {
- float weight, volume;
- int size, letters;
- char name[10]; //数组
- printf("Hi! What's your first name?\n");
- scanf_s("%s", name);
- printf("%s, what's your weight in pounds?\n", name);
- scanf_s("%f", &weight);
- size = sizeof name;
- letters = strlen(name);
- volume = weight / DENSITY;
- printf("Well, %s, your volume is %2.2f cubic feet.\n", name, volume);
- printf("Also, your first name has %d letters,\n", letters);
- printf("and we have %d butes to store it.\n", size);
- return 0;
- }
复制代码
在我编译后,第一次输入参数时,程序就不会继续下去了只会显示
Hi! What's your first name?
abc
然后就不动了。
在vscode里面会显示
0x0F62D4EC (ucrtbased.dll)处(位于 Code list 4.1 talkback.c.exe 中)引发的异常: 0xC0000005: 写入位置 0x00B00000 时发生访问冲突。
并在第一个scanf_s后面打一个×,我用的是vs code 2019。
楼上的两位你们跑偏了。。
回答楼主的问题
scanf_s作为scanf的“安全版”,为了防止溢出,在读取字符串时,需要同时提供字符串长度: - scanf_s("%s", name,sizeof(name));
复制代码
|
|