Difference between strncmp() and strcmp() in C/C++

C++ProgrammingServer Side Programming

strncmp()

The function strncmp() is used to compare left string to right string up to a number. It works same as strcmp(). It returns a value greater than zero when the matching character of left string has greater ASCII value than the character of the right string. Returns a value less than zero when the matching character of left string has lesser ASCII value than the character of the right string.

Here is the syntax of strncmp() in C language,

int strncmp ( const char *leftString, const char *rightString, size_t number );

Here,

  • leftString  − The first string which is to be compared with right string.

  • rightString  − The second string which is used to compare the first string.

  • number − Maximum number of characters to compare.

Here is an example of strncmp() in C language,

Example

 Live Demo

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

int main() {
   char str1[] = "blank";
   char str2[] = "Hello World!";
   int result = strncmp(str1, str2, 1);

   if (result==0)
   printf("Strings are equal");
   else
   printf("Strings are unequal");

   printf("\nValue returned by strcmp() is: %d" , result);
   return 0;
}

Output

Here is the output

Strings are unequal
Value returned by strcmp() is: 26

strcmp()

The function strcmp() is a built-in library function and declared in “string.h” header file. This function is used to compare the string arguments. It compares strings lexicographically which means it compares both the strings character by character. It starts comparing the very first character of strings until the characters of both strings are equal or NULL character is found.

If the first character of both strings are equal, it checks second character and so on. This process will be continued until NULL character is found or both characters are unequal. It returns zero if both strings are identical i.e. characters are same in both strings.

It returns a value greater than zero when the matching character of left string has greater ASCII value than the character of the right string. It returns a value less than zero when the matching character of left string has lesser ASCII value than the character of the right string.

Here is the syntax of strcmp() in C language,

int strcmp(const char *leftString, const char *rightString );

Here,

  • leftString − The first string which is to be compared with right string.

  • rightString − The second string which is used to compare the first string.

Here is an example of strcmp() in C language,

Example

 Live Demo

#include<stdio.h>
#include<string.h>
int main() {
   char str1[] = "hello World!";
   char str2[] = "Hello World!";
   int result = strcmp(str1, str2);

   if (result==0)
   printf("Strings are equal");
   else
   printf("Strings are unequal");

   printf("\nValue returned by strcmp() is: %d" , result);
   return 0;
}

Output

Here is the output:

Strings are unequal
Value returned by strcmp() is: 32
raja
Published on 24-Oct-2018 11:05:17
Advertisements