
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between strncmp and strcmp in C/C++
Both strncmp() and strcmp() are used in C/C++ programs for lexicographical string comparison. The strcmp() compares two strings till the null character is found, whereas strncmp() only compares a specified number of characters.
What is 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.
Syntax
Following is the basic syntax of strncmp() in C/C++:
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.
Example
Following is the illustration of strncmp() function in C/C++ language.
#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
The above code produces the following result −
Strings are unequal Value returned by strcmp() is: 26
#include<iostream> #include<cstring> int main() { char str1[] = "blank"; char str2[] = "Hello World!"; int result = strncmp(str1, str2, 1); if (result==0) std::cout << "Strings are equal"; else std::cout << "Strings are unequal"; std::cout << "\nValue returned by strcmp() is: " << result; return 0; }
Output
The above code produces the following result −
Strings are unequal Value returned by strcmp() is: 26
What is 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.
Syntax
Here is the syntax of strcmp() in C/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.
Example
Following is the C/C++ program to see the execution of strcmp().
#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
The above code produces the following result −
Strings are unequal Value returned by strcmp() is: 32
#include<iostream> #include<cstring> int main() { char str1[] = "hello World!"; char str2[] = "Hello World!"; int result = strcmp(str1, str2); if (result==0) std::cout << "Strings are equal"; else std::cout << "Strings are unequal"; std::cout << "\nValue returned by strcmp() is: " << result; return 0; }
Output
The above code produces the following result −
Strings are unequal Value returned by strcmp() is: 32
Difference between strcmp() and strncmp()
Here, we are providing the tabular structure to learn the differences of these functions:
Feature | 'strcmp()' | 'strncmp()' | Example Use Case |
---|---|---|---|
Purpose | Compares two full strings | Compares up to 'n' characters | Check if two strings are completely equal |
Header File | '<string.h>' | '<string.h>' | Standard C string manipulation |
Syntax | 'strcmp(s1, s2)' | 'strncmp(s1, s2, n)' | Use 'strncmp()' when only a portion needs checking |
Return Value | 0 if equal, <0 if s1<s2, >0 if s1>s2 | Same as 'strcmp()', but only for first 'n' chars | Sort strings or check equality |
Scope | Entire strings | First 'n' characters | Partial match, such as file extension |