<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ### 字符數組 ~~~ char str1[10] = {'h', 'e', 'l', 'l', 'o'}; //后面的元素是 0 0 0 0 0 printf("%s\n", str1); //%s會接受字符串結束標志'\0'之前的所有字符,在ascii碼中就是數字0 ~~~ ``` hello ``` ~~~ char str1[10] = {'h', 'e', 'l', 'l', 'o'}; for (int i = 0; i < sizeof(str1) / sizeof(char); ++i) { printf("%c", str1[i]); } ~~~ 可以看到打印出的是ascii碼中的'\0'就是空字符. ![](https://img.kancloud.cn/3e/82/3e82a1a5ebebda79c58619c5baac480a_246x46.png) ~~~ char str1[10] = {'h', 'e', 'l', 'l', 'o'}; for (int i = 0; i < sizeof(str1) / sizeof(char); ++i) { printf("%d\n", str1[i]); } ~~~ 可以看到打印的都是ascii碼 ``` 104 101 108 108 111 0 0 0 0 0 ``` ### 字符串 ~~~ char str2[10] = "hello"; printf("%s\n", str2); for (int i = 0; i < sizeof(str2) / sizeof(char); ++i) { printf("%c\n", str2[i]); } ~~~ ![](https://img.kancloud.cn/cf/fe/cffe85b2c25b3958b5e1a06c3e73b24a_240x470.png) ### 字符串不指定長度 ~~~ char str2[] = "hello"; printf("%s\n", str2); printf("%d\n", sizeof(str2)); //6個長度 for (int i = 0; i < sizeof(str2) / sizeof(char); ++i) { printf("%c\n", str2[i]); } ~~~ 可以看到程序會自動的幫我們加上'\0' ![](https://img.kancloud.cn/c4/59/c45942b5bb488ef1f6685d7297f77a35_282x356.png) ~~~ char str3[] = {'h', 'e', 'l', 'l', 'o'}; printf("%d\n", sizeof(str3)); //5個長度 for (int i = 0; i < sizeof(str3) / sizeof(char); ++i) { printf("%c\n", str3[i]); } ~~~ ``` 5 h e l l o ``` ### 手動加上\0 ~~~ char str3[] = "he\0llo"; printf("%d\n", sizeof(str3)); printf("%s\n", str3); ~~~ 遇到\0就結束了 ``` 7 he ``` ### 字符數組用%s輸出 ~~~ char str3[] = {'h', 'e', 'l', 'l', 'o'}; printf("%s\n", str3); printf("%d", sizeof(str3)); ~~~ 老師演示的是后面會出現亂碼,因為用%s輸出,必須要找到\0才算結束. 但是這里并沒有. 可能是因為標準變了. **千萬不要這么來使用**. ``` hello 5 ``` ### scanf 的問題 **scanf()函數接收輸入數據時,遇以下情況結束一個數據的輸入** ① 遇空格、“回車”、“跳格”鍵。 ② 遇寬度結束。 ③ 遇非法輸入 ~~~ char str[100]; scanf("%s",str); printf("%s",str); ~~~ ``` hello world hello //只輸出了hello ``` 這樣才可以 ~~~ char str[100]; scanf("%[^\n]",str); printf("%s",str); ~~~ ``` hello world hello world ``` ### 字符串追加 ~~~ char str1[] = "hello"; char str2[] = "world"; char str3[100]; int index = 0; while (str1[index] != '\0') { str3[index] = str1[index]; index++; } while (str2[index - 5] != '\0') { str3[index] = str2[index - 5]; index++; } ~~~ ### 字符串和字符數組在內存中不同的存儲方式
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看