strncmp, _mbsncmp, wcsncmp
Go Up to string.h Index
Header File
string.h, mbstring.h
Category
Memory and String Manipulation Routines, Inline Routines
Prototype
int strncmp(const char *s1, const char *s2, size_t maxlen);
int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t maxlen);
int _mbsncmp(const unsigned char *s1, const unsigned char *s2, size_t maxlen);
#define _mbccmp(__s1, __s2) _mbsncmp((__s1),(__s2),1)
Description
Compares a portion of one string to a portion of another.
strncmp makes the same unsigned comparison as strcmp, but looks at no more than maxlen characters. It starts with the first character in each string and continues with subsequent characters until the corresponding characters differ or until it has examined maxlen characters.
Return Value
- strncmp returns an int value based on the result of comparing s1 (or part of it) to s2 (or part of it):
- < 0 if s1 is less than s2
- == 0 if s1 is the same as s2
- > 0 if s1 is greater than s2
Example
#include <string.h> #include <stdio.h> int main(void) { char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; int ptr; ptr = strncmp(buf2,buf1,3); if (ptr > 0) printf("buffer 2 is greater than buffer 1\n"); else printf("buffer 2 is less than buffer 1\n"); ptr = strncmp(buf2,buf3,3); if (ptr > 0) printf("buffer 2 is greater than buffer 3\n"); else printf("buffer 2 is less than buffer 3\n"); return(0); }
Portability
POSIX | Win32 | ANSI C | ANSI C++ | |
---|---|---|---|---|
strncmp |
+ |
+ |
+ |
+ |
_mbsncmp |
+ |
|||
_mbccmp |
+ |
|||
wcsncmp |
+ |
+ |
+ |