😸
[C] strcmp 関数
機能
2つの文字列を比較し、大小(辞書順)を判定する関数[1]
使用例
abc と abd を比較する
\n は改行を表す エスケープシーケンス[2]
コード
printf("%d\n", strcmp("abc", "abd"));
全文
#include <stdio.h>
#include <string.h>
void main() {
printf("%d\n", strcmp("abc", "abd"));
}
実行結果
-1
変数の文字列 apple と apricot を比較する
コード
char s1[] = "apple";
char s2[] = "apricot";
printf("%d\n", strcmp(s1, s2));
全文
#include <stdio.h>
#include <string.h>
void main() {
char s1[] = "apple";
char s2[] = "apricot";
printf("%d\n", strcmp(s1, s2));
}
実行結果
-15
Discussion