From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/string%2Fbyte%2Fstrncmp.html | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 devdocs/c/string%2Fbyte%2Fstrncmp.html (limited to 'devdocs/c/string%2Fbyte%2Fstrncmp.html') diff --git a/devdocs/c/string%2Fbyte%2Fstrncmp.html b/devdocs/c/string%2Fbyte%2Fstrncmp.html new file mode 100644 index 00000000..d2dc2071 --- /dev/null +++ b/devdocs/c/string%2Fbyte%2Fstrncmp.html @@ -0,0 +1,58 @@ +

strncmp

Defined in header <string.h>
int strncmp( const char* lhs, const char* rhs, size_t count );
+

Compares at most count characters of two possibly null-terminated arrays. The comparison is done lexicographically. Characters following the null character are not compared.

+

The sign of the result is the sign of the difference between the values of the first pair of characters (both interpreted as unsigned char) that differ in the arrays being compared.

+

The behavior is undefined when access occurs past the end of either array lhs or rhs. The behavior is undefined when either lhs or rhs is the null pointer.

+

Parameters

+ + +
lhs, rhs - pointers to the possibly null-terminated arrays to compare
count - maximum number of characters to compare

Return value

Negative value if lhs appears before rhs in lexicographical order.

+

Zero if lhs and rhs compare equal, or if count is zero.

+

Positive value if lhs appears after rhs in lexicographical order.

+

Notes

This function is not locale-sensitive, unlike strcoll and strxfrm.

+

Example

#include <stdio.h>
+#include <string.h>
+ 
+void demo(const char* lhs, const char* rhs, int sz)
+{
+    const int rc = strncmp(lhs, rhs, sz);
+    if (rc < 0)
+        printf("First %d chars of [%s] precede [%s]\n", sz, lhs, rhs);
+    else if (rc > 0)
+        printf("First %d chars of [%s] follow [%s]\n", sz, lhs, rhs);
+    else
+        printf("First %d chars of [%s] equal [%s]\n", sz, lhs, rhs);
+}
+int main(void)
+{
+    const char* string = "Hello World!";
+    demo(string, "Hello!", 5);
+    demo(string, "Hello", 10);
+    demo(string, "Hello there", 10);
+    demo("Hello, everybody!" + 12, "Hello, somebody!" + 11, 5);
+}

Output:

+
First 5 chars of [Hello World!] equal [Hello!]
+First 10 chars of [Hello World!] follow [Hello]
+First 10 chars of [Hello World!] precede [Hello there]
+First 5 chars of [body!] equal [body!]

References

See also

+ + + + +
compares two strings
(function)
+
(C95)
compares a certain amount of characters from two wide strings
(function)
compares two buffers
(function)
compares two strings in accordance to the current locale
(function)
C++ documentation for strncmp
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/string/byte/strncmp +

+
-- cgit v1.2.3