文字列同士を比較する・strcmp

文字列と文字列が同じかどうか比較するにはstrcmpを使用します。
以下にstrcmpを使用した例を記します。


スポンサーリンク

関連記事

strcmpの書式

書式を確認すると以下のようになります。

strcmpのCサンプルコード

filestrcmp.c (改行コードLF)

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

int main(void)
{
    char *a = "abcdefg";
    char *b = "abcdefg";
    char *c = "ABCDEFG";

    if (0 == strcmp(a, b)) {
        printf("strcmp(a, b) : a and b are the same.\n");
    }
    else {
        printf("strcmp(a, b) : a and b are different.\n");
    }

    if (0 == strcmp(b, c)) {
        printf("strcmp(b, c) : a and b are the same.\n");
    }
    else {
        printf("strcmp(b, c) : a and b are different.\n");
    }

    return 0;
}

strcmpのCサンプルコード実行結果

以下にコンパイルおよび実行結果を記します。

$ gcc strcmp.c -o strcmp
$ ./strcmp 
strcmp(a, b) : a and b are the same.
strcmp(b, c) : a and b are different.

以上、strcmpのCサンプルコードでした。


添付ファイル: filestrcmp.c 1023件 [詳細]

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2025-03-12 (水) 10:55:32