Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      strncmp

      From cppreference.com
      <c‎ |string‎ |byte
       
       
       
       
      Defined in header<string.h>
      int strncmp(constchar* lhs,constchar* rhs,size_t count);

      Compares at mostcount characters of two possibly null-terminated arrays. The comparison is done lexicographically. Characters following the null character are not compared.

      The sign of the result is the sign of the difference between the values of the first pair of characters (both interpreted asunsignedchar) that differ in the arrays being compared.

      The behavior is undefined when access occurs past the end of either arraylhs orrhs. The behavior is undefined when eitherlhs orrhs is the null pointer.

      Contents

      [edit]Parameters

      lhs, rhs - pointers to the possibly null-terminated arrays to compare
      count - maximum number of characters to compare

      [edit]Return value

      Negative value iflhs appears beforerhs in lexicographical order.

      Zero iflhs andrhs compare equal, or if count is zero.

      Positive value iflhs appears afterrhs in lexicographical order.

      [edit]Notes

      This function is not locale-sensitive, unlikestrcoll andstrxfrm.

      [edit]Example

      Run this code
      #include <stdio.h>#include <string.h> void demo(constchar* lhs,constchar* rhs,int sz){constint rc= strncmp(lhs, rhs, sz);if(rc<0)printf("First %d chars of [%s] precede [%s]\n", sz, lhs, rhs);elseif(rc>0)printf("First %d chars of [%s] follow [%s]\n", sz, lhs, rhs);elseprintf("First %d chars of [%s] equal [%s]\n", sz, lhs, rhs);}int main(void){constchar* string="Hello World!";    demo(string,"Hello!",5);    demo(string,"Hello",10);    demo(string,"Hello there",10);    demo("Hello, everybody!"+12,"Hello, somebody!"+11,5);}

      Output:

      First 5 chars of [Hello World!] equal [Hello!]First 10 chars of [Hello World!] follow [Hello]First 10 chars of [Hello World!] precede [Hello there]First 5 chars of [body!] equal [body!]

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.24.4.4 The strncmp function (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.24.4.4 The strncmp function (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.24.4.4 The strncmp function (p: 366)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.21.4.4 The strncmp function (p: 329)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.11.4.4 The strncmp function

      [edit]See also

      compares two strings
      (function)[edit]
      (C95)
      compares a certain amount of characters from two wide strings
      (function)[edit]
      compares two buffers
      (function)[edit]
      compares two strings in accordance to the current locale
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/byte/strncmp&oldid=153460"

      [8]ページ先頭

      ©2009-2025 Movatter.jp