C++ strncmp() Function

Definition
In C++, strncmp() compares two C-strings the same way as strcmp(), but it stops after count characters. It also stops early if it reaches a '\0'.
All three arguments are required. A result of 0 means the compared part matches. The rest of each string can still be different. Include <cstring>.
Syntax
Use the third argument when you only want to compare the start of the strings.
strncmp("Hello!", "Hello?", 5);
| Call | What it does |
|---|
strncmp("Hello!", "Hello?", 5) | Compares only the first 5 characters. Those match, so the result is 0. |
Library declarations
int strncmp(const char* lhs, const char* rhs, size_t count);
Parameters
| Parameter | Description |
|---|
| first | Required. The first string. |
| second | Required. The second string. |
| count | Required. The most characters to compare. |
Return Value
| Return Value | Description |
|---|
| 0 | The compared characters are the same. |
| Less than 0 | The first string comes before the second. |
| Greater than 0 | The first string comes after the second. |
C++ Examples
Example 1 — Check a Prefix
See if a string starts with "Hello".
#include <iostream>
#include <cstring>
int main() {
const char* text = "Hello World!";
if (std::strncmp(text, "Hello", 5) == 0) {
std::cout << "Starts with Hello\n";
}
return 0;
}
How It Works
- Only the first 5 characters are compared.
"Hello" matches the start of text.- Result
0 means equal for that prefix.
Example 2 — Same Start, Different Rest
With a small count, longer differences are ignored.
#include <iostream>
#include <cstring>
int main() {
const char* a = "Hello World!";
const char* b = "Hello!";
int r5 = std::strncmp(a, b, 5);
int r10 = std::strncmp(a, b, 10);
std::cout << "First 5: " << (r5 == 0 ? "equal" : "different") << '\n';
std::cout << "First 10: " << (r10 == 0 ? "equal" : "different") << '\n';
return 0;
}
First 5: equal
First 10: different
How It Works
- Both start with
Hello, so count 5 matches. - At count 10, spaces and later characters differ.
count controls how far the compare goes.
Example 3 — Prefix with std::string
Clearer modern alternative for the same idea.
#include <iostream>
#include <string>
int main() {
std::string text = "Hello World!";
if (text.compare(0, 5, "Hello") == 0) {
std::cout << "Starts with Hello\n";
}
return 0;
}
How It Works
compare(0, 5, "Hello") checks positions 0–4.- Return
0 means that slice matches. - Learn
strncmp for C APIs; prefer std::string in new C++.
Frequently Asked Questions
strncmp() compares two C-strings, but it stops after count characters.
Two strings, plus count, the maximum number of characters to compare.
0 when the compared part matches, a negative number when the first comes first, and a positive number when the first comes after.
Only the characters that were compared. The rest of each string can still differ.
strcmp() reads each string until the null. strncmp() also stops after count characters.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful