C++ strncmp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

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.

C++
strncmp("Hello!", "Hello?", 5);
CallWhat it does
strncmp("Hello!", "Hello?", 5)Compares only the first 5 characters. Those match, so the result is 0.
Library declarations
C++
int strncmp(const char* lhs, const char* rhs, size_t count);

Parameters

ParameterDescription
firstRequired. The first string.
secondRequired. The second string.
countRequired. The most characters to compare.

Return Value

Return ValueDescription
0The compared characters are the same.
Less than 0The first string comes before the second.
Greater than 0The first string comes after the second.

C++ Examples

Example 1 — Check a Prefix

See if a string starts with "Hello".

C++
#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

  1. Only the first 5 characters are compared.
  2. "Hello" matches the start of text.
  3. Result 0 means equal for that prefix.

Example 2 — Same Start, Different Rest

With a small count, longer differences are ignored.

C++
#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;
}

How It Works

  1. Both start with Hello, so count 5 matches.
  2. At count 10, spaces and later characters differ.
  3. count controls how far the compare goes.

Example 3 — Prefix with std::string

Clearer modern alternative for the same idea.

C++
#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

  1. compare(0, 5, "Hello") checks positions 0–4.
  2. Return 0 means that slice matches.
  3. 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.

Next: strncpy()

Copy at most n characters into a destination buffer.

strncpy() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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