Javatpoint Logo
Javatpoint Logo

Strncmp() function in C

In this article, we will learn about the strncmp() function with its syntax and examples.

What is Strncmp() function?

In C programming, the string comparison function 'strncmp()' is used to compare two strings character by character, up to a specified length.

Syntax

The strncmp() function can be carried out via the syntax as follows:

The three parameters are:

  • 's1' and 's2' are the pointers to the two strings that the user wants to compare.
  • 'n' defines the number of characters to be compared between the two strings.

Based on the comparisons of both the strings, the function returns an integer, based on the following conditions.

  1. The characteristic compares strings s1 and s2 with the usage of specific encodings, including ASCII, and returns a negative integer if string s1 is less than the string s2.
  2. When comparing two strings, the method returns a positive integer if s1 is obviously greater than s2.
  3. While the comparison of both the strings upto the specified number of characters, if s1 equals s2, the function returns the value of 0.

Example:

A snippet of code showing how to make use of the strncmp() feature:

Output:

The string 'javaTpoint' is greater than the string 'JAVATPOINT' up to the first 5 characters.

Some other points regarding the strncmp() function C:

  • Case-sensitive: In case-sensitive programming languages, the ­case is considered different because the ASCII values are not the same. This strncmp() function compares strings based on the ASCII values of the characters.
  • All versions of the C language and all compilers (including online compilers) released to date support the strncmp() and strcmp() functions of the Standard C Library.
  • The strncmp() function is much better than the strcmp() function. It is because you can use this function to prevent buffer overflows when comparing strings of unknown length. It is because the strncmp() function only handles strings up to a certain number of characters for which the comparison result is passed. As an argument when the function is called.
  • The strncmp() function works on null-terminated strings, which means the strings to be compared must terminate with the null character '\0'.
  • If the argument 'size' is 0, the function will always return 0.
  • If the user wants to compare the two strings without considering the case, the 'strncasecmp()' function can be used instead of strncmp(). Though this strncasecmp() is not a standard C function, it is available on many platforms, including Linux and macOS.
  • The strncmp() finds its applications in sorting the strings in alphabetical or lexicographic order.
  • If the user wants to compare the strings containing null characters, use the memcmp() function. This memcmp() feature takes pointers and a length as arguments and compares the memory at those locations byte-via-byte without regard to null characters.
  • The 'strncmp()' feature also can be utilized in combination with other string features, consisting of 'strncpy()' or 'strcat()', to manipulate or evaluate substrings of large strings.
  • Additionally, this function may be slow, particularly when comparing long strings or when comparing needs to be performed multiple times. We can use efficient comparison algorithms such as Boyer-Moore or Knuth-Morris-Pratt algorithms to overcome this performance.
  • Suppose we set the 'size' or 'n' to a larger value than the length of both strings. In that case, it results in comparing uninitialized memory, thus can lead to undefined or unexpected behaviour.
  • Also, unexpected results may occur if we work on strings containing non-ASCII characters since this function compares the strings lexicographically, meaning that it considers the order of characters based on their ASCII values.
  • The strncmp() characteristic is deterministic in nature, which means that it always returns the identical result for the equal inputs, which allows for trying out and debugging to verify whether given strings are identical or no longer and to pick out the precise position at which those strings vary.

Example:

The following code snippet demonstrates the use of the strncmp() function to check if given two strings are equal:

Output:

The strings 'javatpoint' and 'javatpoint' are equal.

In the above code, both the strings are compared to the length of string 's2' number of characters.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA