|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <string.h> //提供strlen()函数的原型
#define DENSITY 62.4 //人的密度(单位是:英镑/每立方英尺)
int main(void)
{
float weight;
float volume;
int size;
int letters;
char name[40]; // name 有一个有40个字符的数组
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 in.\n", size);
return 0;
}
/*
Win10 VS2019
---------------------
本人答案:
Hi! What's your first name?
sharla
---------------------
书本答案:
Hi! What's your first name?
sharla
sharla, what's your weight in pounds?
139
Well, Sharla, your volume is 2.23 cubic feet.
Aiso, your first name has 6 letters,
and We have 40 bytes to store it in.
*/
本帖最后由 jackz007 于 2020-10-18 12:25 编辑
scanf_s() 在输入字符串时,必须有一个参数,用来指明用于保存字符串变量的字符容量。
为这一句添加蓝色字符部分试试看呢。
scanf_s("%s", name,40) ;
|
|