字符串的输入输出
#include <stdio.h>#include <string.h>
#define DENSITY 62.4
int main()
{
float weight, volume;
int size, letters;
char name;
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 bytes to store it.\n", size);
getchar();
getchar();
return 0;
}
这个程序,我用VS调试的时候,第一个printf可以输出,但是我一输入回车就会出现错误 scanf_s()用于读取字符串时,必须提供一个数字以表明最多读取多少位字符,以防止溢出。
scanf_s("%s", name); 这一行改为 scanf_s("%s", name, 40); 你scanf函数确定不会出错吗?是scanf,不是scanf_s吧? 我输出结果是没问题的
页:
[1]