C++ strncmp() function with example

The strncmp() function in C++ is used for comparing two string and checking if they are equal or not. strcmp() function compares two strings character by character from the first character until the end of one of the string occurs then it returns the result.

Basic Syntax

int strncmp ( const char * str1, const char * str2, size_t num );

And the parameters are:

ParameterDescription
str1This is first string that should be comapared.
str2This is second string that should be comapared.
numThis is maximum number of characters of string to be compared.

Return Value

This function returns the following values:

  • 0 : If both strings are equal means all characters in the string are same then it will return 0.
  • Greater than zero (Positive Integer) : It returns a value greater than zero when the ASCII value of the first unmatched character in str1 is greater than second.
  • Less than zero (Negative) : It returns value less than zero when ASCII value of the first unmatched character in str2 is less than second.

Example

Following is the example for C++ strncmp() function.

The output should be:

LEAVE A REPLY

Please enter your comment!
Please enter your name here