🔧 文字列 ・ string.h ・ C89
strncmp の使い方int strncmp(const char *s1, const char *s2, size_t n);
先頭から最大n文字だけを比較するstrcmpの派生版。プレフィックス一致の判定や、固定長フィールドの比較などによく使われる比較関数。 ファイル拡張子の判定など、決まった長さの接頭辞だけを厳密に比較したい場面で重宝する。
#include <stdio.h>
#include <string.h>
int main(void) {
if (strncmp("hello.txt", "hello", 5) == 0) {
puts("starts with hello");
}
return 0;
}
アプリの関数リファレンスで引く(無料・登録不要) 関数一覧へ
strncmp と一緒に使う関数memcmp
memcpy
memmove
memset
memset_explicit
strcat
strchr
strcmp
strcoll
strcpy
strcspn
strdup
strerror
strlen
strncat
strncpy
もっと先へ:ポインタ・メモリ・ファイル入出力・セキュアコーディングを含む全26トラックと、段位検定・模試のフルセットは完全版に収録しています。