c语言求不同的数据类型在计算机中占的内存:

我想达到的目的是输入一个int就得到int在计算机中占的字节数,然后再输入float,得到float的字节数,但是我发现我写的这个程序不能实现目的:你们能帮我改改吗???... 我想达到的目的是输入一个int就得到int在计算机中占的字节数,然后再输入float,得到float的字节数,但是我发现我写的这个程序不能实现目的:你们能帮我改改吗???、、
#include<stdio.h>
void main()
{
int b;
char c[20];
while(1)
{
printf("\nenter the type of your data:");
gets(c);
puts(c);
b=sizeof(c);
printf("\n%d",b);
}

}
展开
 我来答
roaming_sheep
2012-04-04 · TA获得超过699个赞
知道小有建树答主
回答量:589
采纳率:0%
帮助的人:717万
展开全部
sizeof不是根据“名称”来返回占内存多少,而是根据参数的数据类型来返回

你这里的 sizeof(c),就是返回 char c[20] 占多大空间,和c字符串的内容没有关系。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
bdbuduan
2012-04-04 · TA获得超过650个赞
知道小有建树答主
回答量:369
采纳率:0%
帮助的人:350万
展开全部
#include<stdio.h>
#include<string.h>

int main()
{
int b;
char c[20];
while(1)
{
printf("enter the type of your data:");
gets(c);
puts(c);
if(0==strcmp(c,"int"))
{
b=sizeof(int);
printf("%d\n\n",b);
}
else if(0==strcmp(c,"char"))
{
b=sizeof(char);
printf("%d\n\n",b);
}
else if(0==strcmp(c,"float"))
{
b=sizeof(float);
printf("%d\n\n",b);
}
else if(0==strcmp(c,"double"))
{
b=sizeof(double);
printf("%d\n\n",b);
}
else printf("输入无效!\n\n");

}

return 0;

}
//帮你写好啦
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友46d9a94
2012-04-04 · 超过17用户采纳过TA的回答
知道答主
回答量:98
采纳率:0%
帮助的人:47.3万
展开全部
#include<stdio.h>
void main()
{
int b;
char c[20];
while(1)
{
printf("\nenter the type of your data:");
gets(c);
puts(c);
}
b=sizeof(c);
printf("\n%d",b);

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式