C strncmp() Function Tutorial

In this section, we will learn what the strncmp() function is and how to use it in C.

Comparing String in C: strncmp() Function

Sometimes we want to compare and see whether the first n-numbers of characters in two character-strings are the same or one is greater than the other!

For example:

Are the first 3 characters of “test” and “test” equal?

Are the first 2 characters of the “Hello” and “goodbye” equal?

This is where we can use `strncmp` function.

Note: `cmp` stands for `compare`, the `str` stands for `string` and `n` stands for `number`.

The prototype of this function exists in the `string.h` header and so we need to include the header file in order to use the function.

strncmp() Function Syntax

Here’s the prototype of the function:

int strncmp( const char* lhs, const char* rhs, size_t count );

strncmp() Function Parameters

  • The first parameter of this function is the address of the first character-string.
  • The second parameter of this function is the address of the second character-string.
  • The third parameter of this function is the number of characters that we want to be compared in the character-strings.

strncmp() Function Return Value

The return value of this function is an integer and based on the arguments we put in the function, there are three types of integer value that will return from the function:

  1. 0: if the returned value of the function is 0, it means both the first and the second arguments are exactly the same in the declared number of characters.
  2. Positive value: if the first argument in the function is greater than the second argument in the declared number of characters, the result value is a positive integer.
  3. Negative value: if the first argument in the function is smaller than the second argument in the declared number of characters, the result value is a negative integer.

Note: the comparison is done character by character and that means the first character of the first argument is compared with the first character of the second argument, then the second characters are compared and so on… as long as characters are the same, this comparison goes on to the end of declared number of characters to be compared. If there was a difference between characters, the comparison will stop right there and either a positive or a negative value will return. If all the characters in both character-strings were the same, the end result will be 0 which means they are equal.

Example: comparison of strings in C

#include <stdio.h>
#include <string.h>
int main() {

    char * pFirst = "Hello";
    char * pSecond = "Hello";

    int result = strncmp(pFirst, pSecond,3);


    printf("The result is: %d", result);
    return 0;
}

Output:

The result is: 0

Example: compare two strings in C

#include <stdio.h>
#include <string.h>
int main() {

    char * pFirst = "Hello";
    char * pSecond = "Hello to you";

    int result = strncmp(pFirst, pSecond,4);


    printf("The result is: %d", result);
    return 0;
}

Output:

The result is: 0

In this example because the first 4 characters in the both character-strings are the same, the end result is 0 (which means they are the same)

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies