Difference between strncmp() and strcmp in C/C++ Last Updated : 10 Nov, 2022 Comments Improve Suggest changes Like Article Like Report The basic difference between these two are : strcmp compares both the strings till null-character of either string comes whereas strncmp compares at most num characters of both strings. But if num is equal to the length of either string than strncmp behaves similar to strcmp.Problem with strcmp function is that if both of the strings passed in the argument is not terminated by null-character, then comparison of characters continues till the system crashes. But with strncmp function we can limit the comparison with num parameter. When pass str1 and str2 as parameters to strcmp() function, it compares both the strings character by character till null-character('\0'). In our case, upto character 's' both the strings remain same but after that str1 has character 'h' having ASCII value 104 and str2 has null-character having ASCII value 0. Since the ASCII value of str1 character is greater than ASCII value of str2 character, so strcmp() function returns value greater than zero. Hence, string str1 is greater than string str2 in strcmp() function. When pass these parameters in strncmp() function with the 3rd parameter num upto which want to compare strings then it compares both the strings character by character till num(if num <= length of smallest string) or till null character of smallest string. In our case, both the strings have same character upto num, so strncmp() function returns value zero. Hence, string str1 is equal to string str2 in strncmp() function. CPP // C, C++ program demonstrate difference between // strncmp() and strcmp() #include <stdio.h> #include <string.h> int main() { // Take any two strings char str1[] = "akash"; char str2[] = "akas"; // Compare strings using strncmp() int result1 = strncmp(str1, str2, 4); // Compare strings using strcmp() int result2 = strcmp(str1, str2); // num is the 3rd parameter of strncmp() function if (result1 == 0) printf("str1 is equal to str2 upto num characters\n"); else if (result1 > 0) printf("str1 is greater than str2\n"); else printf("str2 is greater than str1\n"); printf("Value returned by strncmp() is: %d\n", result1); if (result2 == 0) printf("str1 is equal to str2\n"); else if (result2 > 0) printf("str1 is greater than str2\n"); else printf("str2 is greater than str1\n"); printf("Value returned by strcmp() is: %d", result2); return 0; } Output: str1 is equal to str2 upto num characters Value returned by strncmp() is: 0 str1 is greater than str2 Value returned by strcmp() is: 104 Let us see the differences in a tabular form -: strncmp()strcmp1.It is a C library Function.It is library function in C and C++2. Its syntax is -: strncmp(const char *str1, const char *str2, size_t n) Its syntax is -: int strcmp ( const char * str1, const char * str2 ); 3.It takes two parameters that is string1 and string2This function performs a binary comparison of the characters.4.Its return value is integer type.It perform operations until NULL is reached. Comment More infoAdvertise with us A AKASH GUPTA 6 Improve Article Tags : C++ C-String Practice Tags : CPP Explore C++ Programming Language 5 min read C++ OverviewIntroduction to C++ Programming Language 3 min read Features of C++ 5 min read History of C++ 7 min read Interesting Facts about C++ 2 min read Setting up C++ Development Environment 8 min read Difference between C and C++ 3 min read C++ BasicsUnderstanding First C++ Program 4 min read C++ Basic Syntax 4 min read C++ Comments 3 min read Tokens in C 4 min read C++ Keywords 2 min read Difference between Keyword and Identifier in C 3 min read C++ Variables and ConstantsC++ Variables 4 min read Constants in C 4 min read Scope of Variables in C++ 7 min read Storage Classes in C++ with Examples 6 min read Static Keyword in C++ 5 min read C++ Data Types and LiteralsC++ Data Types 7 min read Literals in C 4 min read Derived Data Types in C++ 4 min read User Defined Data Types in C++ 4 min read Data Type Ranges and Their Macros in C++ 3 min read C++ Type Modifiers 4 min read Type Conversion in C++ 4 min read Casting Operators in C++ 5 min read C++ OperatorsOperators in C++ 9 min read C++ Arithmetic Operators 4 min read Unary Operators in C 5 min read Bitwise Operators in C 6 min read Assignment Operators in C 4 min read C++ sizeof Operator 3 min read Scope Resolution Operator in C++ 4 min read C++ Input/OutputBasic Input / Output in C++ 5 min read cin in C++ 4 min read cout in C++ 2 min read Standard Error Stream Object - cerr in C++ 2 min read Manipulators in C++ 4 min read C++ Control StatementsDecision Making in C (if , if..else, Nested if, if-else-if ) 7 min read C++ if Statement 3 min read C++ if else Statement 3 min read C++ if else if Ladder 3 min read Switch Statement in C++ 5 min read Jump statements in C++ 4 min read C++ Loops 7 min read for Loop in C++ 6 min read Range-Based for Loop in C++ 3 min read C++ While Loop 3 min read C++ do while Loop 4 min read C++ FunctionsFunctions in C++ 8 min read return Statement in C++ 4 min read Parameter Passing Techniques in C 3 min read Difference Between Call by Value and Call by Reference in C 4 min read Default Arguments in C++ 5 min read Inline Functions in C++ 6 min read Lambda Expression in C++ 4 min read C++ Pointers and ReferencesPointers and References in C++ 5 min read C++ Pointers 8 min read Dangling, Void , Null and Wild Pointers in C 6 min read Applications of Pointers in C 4 min read Understanding nullptr in C++ 3 min read References in C++ 5 min read Can References Refer to Invalid Location in C++? 2 min read Pointers vs References in C++ 5 min read Passing By Pointer vs Passing By Reference in C++ 5 min read When do we pass arguments by pointer? 5 min read Like