“C言語でのstrcmpの使い方はどうですか?”

strcmp関数は、2つの文字列の大小を比較するための関数であり、そのプロトタイプは次の通りです :

int strcmp(const char *s1, const char *s2);

整数型の戻り値を持ち、2つの文字列のサイズの関係を表します。具体的な使用方法は次のとおりです:

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "hello";
    char str2[] = "world";

    int result = strcmp(str1, str2);

    if (result < 0) {
        printf("str1 is less than str2\n");
    } else if (result > 0) {
        printf("str1 is greater than str2\n");
    } else {
        printf("str1 is equal to str2\n");
    }

    return 0;
}

結果は、str1がstr2より小さいと出力されます。同じ文字列の場合は、str1がstr2と等しいと出力されます。str1がstr2より大きい場合は、str1がstr2よりも大きいと出力されます。

bannerAds