Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


strcmp(3) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ATTRIBUTES |VERSIONS |STANDARDS |HISTORY |EXAMPLES |SEE ALSO |COLOPHON

strcmp(3)                Library Functions Manualstrcmp(3)

NAME        top

       strcmp, strncmp - compare two strings

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <string.h>int strcmp(const char *s1, const char *s2);int strncmp(size_t n;const chars1[n], const chars2[n], size_tn);

DESCRIPTION        top

       Thestrcmp() function compares the two stringss1 ands2.  The       locale is not taken into account (for a locale-aware comparison,       seestrcoll(3)).  The comparison is done using unsigned       characters.strcmp() returns an integer indicating the result of the       comparison, as follows:       •  0, if thes1 ands2 are equal;       •  a negative value ifs1 is less thans2;       •  a positive value ifs1 is greater thans2.       Thestrncmp() function is similar, except it compares only the       first (at most)n bytes ofs1 ands2.

RETURN VALUE        top

       Thestrcmp() andstrncmp() functions return an integer less than,       equal to, or greater than zero ifs1 (or the firstn bytes       thereof) is found, respectively, to be less than, to match, or be       greater thans2.

ATTRIBUTES        top

       For an explanation of the terms used in this section, seeattributes(7).       ┌──────────────────────────────────────┬───────────────┬─────────┐       │InterfaceAttributeValue│       ├──────────────────────────────────────┼───────────────┼─────────┤       │strcmp(),strncmp()                  │ Thread safety │ MT-Safe │       └──────────────────────────────────────┴───────────────┴─────────┘

VERSIONS        top

       POSIX.1 specifies only that:              The sign of a nonzero return value shall be determined by              the sign of the difference between the values of the first              pair of bytes (both interpreted as typeunsigned char) that              differ in the strings being compared.       In glibc, as in most other implementations, the return value is       the arithmetic result of subtracting the last compared byte ins2       from the last compared byte ins1.  (If the two characters are       equal, this difference is 0.)

STANDARDS        top

       C11, POSIX.1-2008.

HISTORY        top

       POSIX.1-2001, C89, SVr4, 4.3BSD.

EXAMPLES        top

       The program below can be used to demonstrate the operation ofstrcmp() (when given two arguments) andstrncmp() (when given       three arguments).  First, some examples usingstrcmp():           $./string_comp ABC ABC;           <str1> and <str2> are equal           $./string_comp ABC AB;      # 'C' is ASCII 67; 'C' - '\0' = 67           <str1> is greater than <str2> (67)           $./string_comp ABA ABZ;     # 'A' is ASCII 65; 'Z' is ASCII 90           <str1> is less than <str2> (-25)           $./string_comp ABJ ABC;           <str1> is greater than <str2> (7)           $./string_comp $'\201' A;   # 0201 - 0101 = 0100 (or 64 decimal)           <str1> is greater than <str2> (64)       The last example usesbash(1)-specific syntax to produce a string       containing an 8-bit ASCII code; the result demonstrates that the       string comparison uses unsigned characters.       And then some examples usingstrncmp():           $./string_comp ABC AB 3;           <str1> is greater than <str2> (67)           $./string_comp ABC AB 2;           <str1> and <str2> are equal in the first 2 bytesProgram source       /* string_comp.c          Licensed under GNU General Public License v2 or later.       */       #include <stdio.h>       #include <stdlib.h>       #include <string.h>       int       main(int argc, char *argv[])       {           int res;           if (argc < 3) {               fprintf(stderr, "Usage: %s <str1> <str2> [<n>]\n", argv[0]);               exit(EXIT_FAILURE);           }           if (argc == 3)               res = strcmp(argv[1], argv[2]);           else               res = strncmp(argv[1], argv[2], atoi(argv[3]));           if (res == 0) {               printf("<str1> and <str2> are equal");               if (argc > 3)                   printf(" in the first %d bytes\n", atoi(argv[3]));               printf("\n");           } else if (res < 0) {               printf("<str1> is less than <str2> (%d)\n", res);           } else {               printf("<str1> is greater than <str2> (%d)\n", res);           }           exit(EXIT_SUCCESS);       }

SEE ALSO        top

memcmp(3),strcasecmp(3),strcoll(3),string(3),strncasecmp(3),strverscmp(3),wcscmp(3),wcsncmp(3),ascii(7)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-06-28strcmp(3)

Pages that refer to this page:bash(1)hsearch(3)memcmp(3)qsort(3)scandir(3)selinux_file_context_cmp(3)strcasecmp(3)strcoll(3)string(3)strverscmp(3)strxfrm(3)wcscmp(3)wcsncmp(3)signal-safety(7)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp