近几天学习C语言的困惑,请指教!

[复制链接]
2082|5
 楼主| erkin 发表于 2007-8-25 23:07 | 显示全部楼层 |阅读模式
#include&nbsp;&ltstdio.h&gt<br />main()<br />{<br />&nbsp;char&nbsp;C[15];<br />&nbsp;scanf(&quot;%s&quot;,C);<br />&nbsp;printf(&quot;%s&nbsp;
&quot;,C);<br />&nbsp;while&nbsp;(1);<br />}<br />当我输入China&quot;空格&quot;Foot的时候,Foot就显示不出来,我不知道为何!<br />编译环境为VC6.0,scanf,printf的函数原型俺也看不到,请高手来指教吧.<br />和&quot;空格&quot;代表的是一样的意思么?
 楼主| erkin 发表于 2007-8-26 11:25 | 显示全部楼层

在贴一个俺搞不懂的问题

#include&nbsp;&ltstdio.h&gt<br />#define&nbsp;uchar&nbsp;unsigned&nbsp;char<br /><br />void&nbsp;func(char&nbsp;str[100])<br />{<br />printf(&quot;%d&nbsp;
&quot;,sizeof(str));/*100&nbsp;&nbsp;&nbsp;4*/<br />}<br /><br />void&nbsp;main()<br />{<br />char&nbsp;str[&nbsp;]=&quot;Hello&quot;;<br />char&nbsp;*p=str;<br />int&nbsp;n=10;<br /><br />printf(&quot;%d&nbsp;
&quot;,sizeof(char));/*1&nbsp;&nbsp;1*/&nbsp;<br />printf(&quot;%d&nbsp;
&quot;,sizeof(int));&nbsp;/*2&nbsp;&nbsp;4*/<br />printf(&quot;%d&nbsp;
&quot;,sizeof(long));/*4&nbsp;&nbsp;4*/<br />printf(&quot;%d&nbsp;
&quot;,sizeof(float));/*4&nbsp;&nbsp;4*/<br /><br />printf(&quot;%d&nbsp;
&quot;,sizeof(str));/*5&nbsp;&nbsp;&nbsp;6*/<br />printf(&quot;%d&nbsp;
&quot;,sizeof(p));/*2&nbsp;&nbsp;&nbsp;4*/<br />printf(&quot;%d&nbsp;
&quot;,sizeof(n));/*2&nbsp;&nbsp;&nbsp;4*/<br />func(str);<br />while&nbsp;(1);&nbsp;<br />}<br />其中/*4&nbsp;&nbsp;4*/,前边的是我合计的结果,后边是实际结果.有几个地方尤其搞不懂.<br />例如printf(&quot;%d&nbsp;
&quot;,sizeof(int));&nbsp;/*2&nbsp;&nbsp;4*/,结果应该是2呀,为什么得出的是4呢?<br />sizeof是求数据类型等的字节数运算符,那么str[]的字节数应不应该算上呢?<br />请求指教.<br />编译环境为VC++6.0
 楼主| erkin 发表于 2007-8-27 08:33 | 显示全部楼层
computer00 发表于 2007-8-27 09:06 | 显示全部楼层

晕...

void&nbsp;func(char&nbsp;str[100])<br />{<br />printf(&quot;%d&nbsp;
&quot;,sizeof(str));/*100&nbsp;&nbsp;&nbsp;4*/<br />//这里str是一个指针,在X86上就是4<br />}<br /><br />void&nbsp;main()<br />{<br />char&nbsp;str[&nbsp;]=&quot;Hello&quot;;<br />char&nbsp;*p=str;<br />int&nbsp;n=10;<br /><br />printf(&quot;%d&nbsp;
&quot;,sizeof(char));/*1&nbsp;&nbsp;1*/&nbsp;<br />printf(&quot;%d&nbsp;
&quot;,sizeof(int));&nbsp;/*2&nbsp;&nbsp;4*/<br />//在X86上,是32位的处理器,所以int就是32位的,4字节,short&nbsp;int才是16位<br />printf(&quot;%d&nbsp;
&quot;,sizeof(long));/*4&nbsp;&nbsp;4*/<br />printf(&quot;%d&nbsp;
&quot;,sizeof(float));/*4&nbsp;&nbsp;4*/<br /><br />printf(&quot;%d&nbsp;
&quot;,sizeof(str));/*5&nbsp;&nbsp;&nbsp;6*/<br />//背后的结束符当然要算,不然它存哪?<br />printf(&quot;%d&nbsp;
&quot;,sizeof(p));/*2&nbsp;&nbsp;&nbsp;4*/<br />//同样,p是指针,当然是4字节<br />printf(&quot;%d&nbsp;
&quot;,sizeof(n));/*2&nbsp;&nbsp;&nbsp;4*/<br />//int是32位的<br /><br />func(str);<br />while&nbsp;(1);&nbsp;<br />}<br />
 楼主| erkin 发表于 2007-8-27 11:07 | 显示全部楼层

re

谢谢COMPUTER的解释,明白了。<br />那么第一个问题怎么解释呢?
computer00 发表于 2007-8-28 02:07 | 显示全部楼层

scanf和printf同时使用时好象有时就会出问题

不要用scanf,换个getchar什么的试试
您需要登录后才可以回帖 登录 | 注册

本版积分规则

35

主题

147

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部