strcmp関数 - 文字列を比較する
strcmp関数は、文字列を比較する関数です。辞書順で「s1」が「s2」より大きい場合は、正の数を、同じ場合は0を、小さい場合は負の数を返します。
「string.h」をインクルードすると使えます。
#include <string.h> int strcmp(const char *s1, const char *s2);
C言語の文字列は「\0」で終わるという約束事があります。strcmp関数は、この約束事を前提として、文字列の比較を行います。
strcmp関数のサンプル
strcmp関数で文字列を比較するサンプルです。一致した場合に、Matchと表示します。
#include <stdio.h>
#include <string.h>
int main(void) {
const char* message = "Hello";
// 文字列を比較
if (strcmp(message, "Hello") == 0) {
printf("Match\n");
}
else {
printf("Not Match\n");
}
}
C言語ゼミ


