strcmp

Section: C Library Functions (3)
Updated: 2026-03-16
Index JM Home Page roff page
 

名前

strcmp, strncmp - 二つの文字列を比べる  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <string.h>

int strcmp(const char *s1, const char *s2);
int strncmp(const char s1[], const char s2[], size_t n);
 

説明

strcmp()
The strcmp() function compares the two strings s1 and s2, as if by calling memcmp(3).
The locale is not taken into account (for a locale-aware comparison, see strcoll(3)).
It is equivalent to
memcmp(s1, s2, MIN(strlen(s1),strlen(s2))+1)
strncmp()
strncmp() 関数は、s1 と s2 の最初の n バイトだけを比較する ことを除けば、strcmp()と同様である。
It is equivalent to
memcmp(s1, s2, MIN(MIN(strnlen(s1,n),strnlen(s2,n))+1, n))
 

返り値

strcmp() 関数と strncmp() 関数は整数を返す。 この整数は、ゼロよりも、1)小さい、2)等しい、3)大きいのいずれかである。 それぞれは、s1(または、この文字列の最初の n バイト)が s2 よりも、1)小さい、2)等しい、3)大きいに対応している。  

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
strcmp(), strncmp() Thread safetyMT-Safe
 

バージョン

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 type unsigned 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 in s2 from the last compared byte in s1. (If the two characters are equal, this difference is 0.)  

標準規格

C11, POSIX.1-2008.  

歴史

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

例

The program below can be used to demonstrate the operation of strcmp() (when given two arguments) and strncmp() (when given three arguments). First, some examples using strcmp():

$ ./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 uses bash(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 using strncmp():

$ ./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 bytes
 

プログラムのソース

/* 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);
}
 

関連項目

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


 

Index

名前
ライブラリ
書式
説明
返り値
属性
バージョン
標準規格
歴史
例
プログラムのソース
関連項目

This document was created by man2html, using the manual pages.