c语言小问题

LI-O 2013-01-20 04:07:54
话不多说。。。。。:

#include<stdio.h>
#include<string.h>
#define DENSITY 62.4
int main()
{
float weight,volume;
int size,letters;
char name[5];
printf("HI!what's your weight in pounds?\n",name);
scanf("%f",&weight);
size=sizeof name;
letters=strlen(name);
volume=weight/DENSITY;

printf("well,%s,you volume is %2.2f cubic feet.\n",name,volume);
printf("Also,you first name has %d letters,\n",letters);
printf("and we have %d bytes to store it in.\n",size);
return 0;
}
其中的
size=sizeof name 中。。。sizeof 哪里来的。?? 前面找不到有关定义。。
还有。。
刚刚发现 有地方忘了输入。。(对比着教科书写的。。囧)。但是不耽搁输出。。
在输入的地方。填写数字或字母的话。。在
在输出中
HI!what's your weight in pounds?
www
well,諏噕德d傀b唙,you volume is 0.00 cubic feet.
Also,you first name has 21 letters,
and we have 5 bytes to store it in.
请按任意键继续. . .
well后面的东西是不是字符的随机??
那为什么在
Also,you first name has 21 letters,中 21 不变。。不管输入什么。。
当然。。也许是因为 size=sizeof name;,

好吧。。下面是正常的代码。。
#include<stdio.h>
#include<string.h>
#define DENSITY 62.4
int main()
{
float weight,volume;
int size,letters;
char name[40];

printf("HI!what's your first name?\n",name);
scanf("%s",name);
printf("%s,what's your weght in pounds?\n",name);
scanf("%f",&weight);
size=sizeof name;
letters=strlen(name);
volume=weight/DENSITY;

printf("well,%s,you volume is %2.2f cubic feet.\n",name,volume);
printf("Also,you first name has %d letters,\n",letters);
printf("and we have %d bytes to store it in.\n",size);
return 0;
}
在这里///。。。
char name[40];中,【a】的数字是a个字符的数组。。那么
这里面的40 不是固定的。。。 在书中写这个的原因///(也许是因为40 好看。。)
我发现当数字为1时(数字小),,输入的名字字母多时会以另一个样子输出。。。(乱码)。。
这是乱码 还是ASIIC码???
另外。。
在scanf("%s",name);中。。 name不需要&符号。。是不是因为 字符而非数字的关系。。




就这么多。。。代码太长////
本人又太菜。。。还请大家解释。。。。感激不尽
...全文
92 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lee_鹿游原 2013-01-20
  • 打赏
  • 举报
回复
引用 2 楼 w15tp 的回复:
谢拉。。 在网上查了下 sizeof 还是看不懂。。。 晕啊
sizeof是C/C++中的一个操作符(operator),内建的支持。 楼主你怎么不问, 我写一个 int 还要包含头文件呢?
LI-O 2013-01-20
  • 打赏
  • 举报
回复
谢拉。。 在网上查了下 sizeof 还是看不懂。。。 晕啊
lee_鹿游原 2013-01-20
  • 打赏
  • 举报
回复
sizeof是关键字,不需要头文件。 char name[40];固定数组长度40,个数根据需求随意定。 因为name是数组名,数组名本身就是地址,所以不需要&符号。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧