C strncmp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

In C, strncmp() compares two C-strings like strcmp(), but it stops after n characters or at a '\0'. Letter case still matters.

You pass both strings and the count. All three are required. A result of 0 means only the compared part matches, not always the whole strings. Include <string.h>.

Syntax

Pass both strings and the count. Start with this call.

C
strncmp("Hello", "Help!", 3);
CallWhat it does
strncmp("Hello", "Help!", 3)Compares only the first 3 characters. Here that prefix matches.

Include <string.h>. Only the sign of the result is portable.

Library declarations
C
int strncmp(const char *s1, const char *s2, size_t n);

Parameters

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

Return Value

Return ValueDescription
0The compared part matches.
Less than 0The first string comes first in the compared part.
Greater than 0The first string comes after in the compared part.

C Examples

Example 1 — Equal Within n

Two strings can differ later and still compare equal for a small n.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    int r = strncmp("Hello", "Help!", 3);

    if (r == 0) {
        printf("equal within n\n");
    } else {
        printf("different\n");
    }
    return 0;
}

How It Works

  1. Limit to 3. Only H, e, l are compared.
  2. All three match. Return value is 0.
  3. Later difference ignored. Index 3 is l vs p, but n already stopped.
  4. Not full equality. Use strcmp when the whole string must match.

Example 2 — Prefix Check

A common pattern: test whether a string starts with a fixed prefix.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *line = "GET /home";

    if (strncmp(line, "GET ", 4) == 0) {
        printf("starts with GET\n");
    } else {
        printf("other command\n");
    }
    return 0;
}

How It Works

  1. Pick prefix length. "GET " is 4 characters.
  2. Compare only those. G E T and space match the start of line.
  3. Return 0. The prefix test succeeds.
  4. Rest ignored. /home is not part of the comparison.

Example 3 — Ordering Within n

When the first differing character falls inside n, the sign of the result shows which string is smaller.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    int r = strncmp("Apple", "Apricot", 3);

    if (r < 0) {
        printf("s1 is less\n");
    } else if (r > 0) {
        printf("s1 is greater\n");
    } else {
        printf("equal within n\n");
    }
    return 0;
}

How It Works

  1. Compare A, p. First two characters match.
  2. Third differs. 'p' vs 'r' (p comes before r).
  3. Negative result. s1 is less than s2 within n.
  4. Do not rely on exact ±1. Only the sign matters for ordering.

Frequently Asked Questions

strncmp() compares at most n characters of two C-strings and returns 0, less than 0, or greater than 0.
All three are required. first, second, and count control the compare.
0 means the compared part matches. The sign tells which side comes first.
Include <string.h>.
No. 0 means only the compared part matches.

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.

9 people found this page helpful