C语言:字符数组的输入输出

论坛 期权论坛     
匿名小用户   2019-10-20 18:18   285   0
<p id="main-toc"><strong>目录</strong></p>

<p id="%E7%94%A8printf%E8%BE%93%E5%87%BA-toc" style="margin-left:80px;"><a data-token="aebf828283e41bd6b9af71c980731b51" href="https://blog.csdn.net/weixin_42072280/article/details/85111577#%E7%94%A8printf%E8%BE%93%E5%87%BA" rel="nofollow">用printf输出</a></p>

<p id="%E7%94%A8scanf%E8%BE%93%E5%85%A5-toc" style="margin-left:80px;"><a data-token="ec47c30e52fa4e8f77f22205e1d77358" href="https://blog.csdn.net/weixin_42072280/article/details/85111577#%E7%94%A8scanf%E8%BE%93%E5%85%A5" rel="nofollow">用scanf输入</a></p>

<p id="%E7%94%A8gets()%E8%BE%93%E5%85%A5-toc" style="margin-left:80px;"><a data-token="9ff3cd23f4cd77c480ba825b93f66a4b" href="https://blog.csdn.net/weixin_42072280/article/details/85111577#%E7%94%A8gets()%E8%BE%93%E5%85%A5" rel="nofollow">用gets输入</a></p>

<p id="%E7%94%A8puts%E8%BE%93%E5%87%BA-toc" style="margin-left:80px;"><a data-token="fd08874e9e4dce759596b57b6b8cdb36" href="https://blog.csdn.net/weixin_42072280/article/details/85111577#%E7%94%A8puts%E8%BE%93%E5%87%BA" rel="nofollow">用puts输出</a></p>

<hr id="hr-toc"><table border="1" cellpadding="1" cellspacing="1"><tbody><tr><td>
   <h3 id="%E7%94%A8printf%E8%BE%93%E5%87%BA">用printf输出</h3>

   <p>输出方法1:printf 逐个字符输出。</p>

   <blockquote>
   <p>void main(void){<br>
     char c[] = "I am happy";<br>
    int i;</p>

   <p></p>

   <p> for(i =0; i &lt; 10; i++){<br>
     printf("%c", c[i]);<br>
    }<br>
    printf("\n");</p>

   <p>}</p>
   </blockquote>

   <p>输出方法2:printf 将整个字符串一次输出。</p>

   <blockquote>
   <p>void main(void){<br>
     char c[] = "I am happy";<br>
    <br>
    printf("%s", c);  //必须得是字符数组名,c.</p>

   <p>}</p>
   </blockquote>

   <h3 id="%E7%94%A8scanf%E8%BE%93%E5%85%A5">用scanf输入</h3>

   <p>如果scanf中%d是连着写的如“%d%d%d”,在输入数据时,数据之间不可以用逗号分隔,只能用<strong>空白字符(空格或tab键或者回车键)</strong>分隔——“2 (空格)3(tab) 4” 或 “2(tab)3(回车)4”等。若是“%d,%d,%d”,则在输入数据时需要加“,”,如“2,3,4”。</p>

   <p>输入方法1:scanf 逐个字符输入。</p>

   <p></p>

   <blockquote>
   <p>void main(void){<br>
    char c[10];<br>
    int i;<br>
    <br>
    printf("请输入多个字符(不多于10个):");<br>
    for(i = 0; i &lt; 10; i++){<br>
     scanf("%c", &amp;c[i]);<br>
    }<br>
    printf("输入的多个字符为:%s", c);  <br>
   }</p>
   </blockquote>

   <p>输入方法2:scanf 输入一个字符串。</p>

   <blockquote>
   <p>void main(void){<br>
    char c[10];<br>
    <br>
    printf("请输入多个字符(不多于10个):");<br>
    <span style="color:#f33b45;">scanf("%s", c);</span></p>

   <p><br>
    printf("输入的多个字符为:%s", c);  <br>
   }</p>
   </blockquote>

   <p><img alt="" class="blockcode" height="135" src="https://201907.oss-cn-shanghai.aliyuncs.com/cs/5606289-7c61a506d6d6ac9bfa33c8666471d5b1.png" width="595"></p>

   <p><span style="color:#f33b45;">scanf("%s", c);</span></p>

   <p><span style="color:#f33b45;">//系统自动在China后面加一个'\0'结束符。</span></p>

   <p><span style="color:#f33b45;">printf("%s", c);</span></p>

   <p><span style="color:#f33b45;">//按字符数组名c找到其数组起始地址,然后逐个输出其中的字符,直到遇到'\0'为止。</span></p>

   <p></p>

   <p><img alt="" class="blockcode" height="120" src="https://201907.oss-cn-shanghai.aliyuncs.com/cs/5606289-4d1bb90ca61c3f0a3bd76d8c44c90934.png" width="513"></p>

   <p></p>

   <p><span style="color:#f33b45;">为什么只输出一个anan呢?</span></p>

   <p><span style="color:#f33b45;">由于系统把空格字符作为输入的字符串之间的分隔符,因此只将空格前的字符anan送到数组c中。</span></p>

   <p></p>

   <p><strong><span style="color:#f33b45;">//数组中未被赋值的元素的值自动置'\0'。</span></strong></p>

   <p><span style="color:#f33b45;">由于把"anan"作为一个字符串处理,故在其后加</span><span style="color:#f33b45;">'\0。c数组状态如下:</span></p>

   <table border="1" cellspacing="0" style="width:228.3pt;"><tbody><tr><td style="vertical-align:top;width:22.2pt;">
      <p style="margin-left:0pt;"><strong><strong>0</strong></strong></p>
      </td>
      <td style="vertical-align:top;width:20.55pt;">
      <p style="margin-left:0pt;"><strong><strong>1</strong></strong></p>
      </td>
      <td style="vertical-align:top;width:19.45pt;">
      <p style="margin-left:0pt;"><strong><strong>2</strong></strong></p>
      </td>
      <td style="vertical-align:top;width:20pt;">
      <p style="margin-left:0pt;"><strong><strong>3</strong></strong></p>
      </td>
      <td style="vertical-align:top;width:23.35pt;">
      <p style="margin-left:0pt;"><strong><strong>4</strong></strong></p>
      </td>
      <td style="vertical-align:top;width:23.3pt;">
      <p style="margin-left:0pt;"><strong><strong>5</strong></strong></p>
      </td>
      <td style="vertical-align:top;width:25pt;">
      <p style="margin-left:0pt;"><strong><strong>6</strong></strong></p>
      </td>
      <td style="vertical-align:top;wi
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:50
帖子:1079
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP