Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Medium
  • Last Updated : 01 Feb, 2018
Geek Week

Prerequisites : strncmp, strcmp

The basic difference between these two are :

  1. 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.
  2. 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.

// 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");
        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");
        printf("str2 is greater than str1\n");
    printf("Value returned by strcmp() is: %d", result2);
    return 0;


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

This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Take a step-up from those “Hello World” programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.

My Personal Notes arrow_drop_up
Recommended Articles
Page :