<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C `strncmp()`函數 > 原文: [https://beginnersbook.com/2017/11/c-strncmp-function/](https://beginnersbook.com/2017/11/c-strncmp-function/) 在上一個教程中,我們討論了[`strcmp()`函數](https://beginnersbook.com/2017/11/c-strcmp-function/),它用于比較兩個字符串。在本指南中,我們將討論 **`strncmp()`函數**,它與`strcmp()`相同,但`strncmp()`比較僅限于函數調用期間指定的字符數。例如,`strncmp(str1, str2, 4)`僅比較字符串`str1`和`str2`的前四個字符。 ## C `strncmp()`函數聲明 ```c int strncmp(const char *str1, const char *str2, size_t n) ``` `str1` - 第一個字符串 `str2` - 第二個字符串 `n` - 需要比較的字符數。 ## `strncmp()`的返回值 此函數僅比較字符串的前`n`個(指定數量)字符,并根據比較返回以下值。 * `0`,如果字符串`str1`和`str2`都相等 * `> 0`,如果`str1`的第一個不匹配字符的 ASCII 值大于`str2` * `< 0`,如果`str1`的第一個不匹配字符的 ASCII 值小于`str2` ## 例 1:C 中的`strncmp()`函數 ```c #include <stdio.h> #include <string.h> int main () { char str1[20]; char str2[20]; int result; //Assigning the value to the string str1 strcpy(str1, "hello"); //Assigning the value to the string str2 strcpy(str2, "helLO WORLD"); //This will compare the first 3 characters result = strncmp(str1, str2, 3); if(result > 0) { printf("ASCII value of first unmatched character of str1 is greater than str2"); } else if(result < 0) { printf("ASCII value of first unmatched character of str1 is less than str2"); } else { printf("Both the strings str1 and str2 are equal"); } return 0; } ``` 輸出: ```c Both the strings str1 and str2 are equal ``` ## `strncmp()`函數的示例 2 讓我們稍微改變上面的例子。這里我們比較字符串的前四個字符。 ```c #include <stdio.h> #include <string.h> int main () { char str1[20]; char str2[20]; int result; strcpy(str1, "hello"); strcpy(str2, "helLO WORLD"); //This will compare the first 4 characters result = strncmp(str1, str2, 4); if(result > 0) { printf("ASCII value of first unmatched character of str1 is greater than str2"); } else if(result < 0) { printf("ASCII value of first unmatched character of str1 is less than str2"); } else { printf("Both the strings str1 and str2 are equal"); } return 0; } ``` 輸出: ```c ASCII value of first unmatched character of str1 is greater than str2 ```
                  <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>

                              哎呀哎呀视频在线观看