strncmp() & strcmp

i want to know the difference between strncmp() & strcmp in c/c++

  • Priti Yadav
  • Priti Yadav
  • |
  • 20 Feb
  • |
  • 180 Views
  • |
  • 14 Answers

text limit upto 4000

  • 1

    The function int strcmp(const char *str1, const char *str2); compares string pointed by str1 with string pointed by str2. This function compares both string character by character. It will continue comparison until the characters mismatch or until a terminating null-character is reached.

    The function int strncmp(const char *str1, const char *str2, size_t n); compares first n characters string pointed by str1 with first n characters of string pointed by str2. It will continue till n characters or until the characters mismatch or until a terminating null-character is reached before n characters.

    • Voted By:
    • A S

  • 0

    Strcmp helps us to compare string and return 0 if it doesn't match Strncmp helps us to compare character of 2 strings


  • 0

    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.


  • 0

    strncmp() -- it wii used for the comparision of two strings for  'n ' elements
    strcmp() -- it will used for the comparision b/t two strings in the all elements


  • 0

    The function int strcmp(const char *str1, const char *str2); compares string pointed by str1 with string pointed by str2. This function compares both string character by character. It will continue comparison until the characters mismatch or until a terminating null-character is reached.

    The function int strncmp(const char *str1, const char *str2, size_t n); compares first n characters string pointed by str1 with first n characters of string pointed by str2. It will continue till n characters or until the characters mismatch or until a terminating null-character is reached before n characters.


  • 0

    strcmp():is used to compair 1st n characters of the string ;
    where as strcmp(): is used to compair whole string


  • 0

    Diffrence between strcmp and strncmp function is that strncmp compares only first n characters whereas strcmp function compares characters till end of the string.


  • 0

    Strcmp will compare two strings by charecter by charecter and returns 0 as well as some other ASCII difference.strncmp also perform same task but it will compare first n characters.


  • 0

    The C library function int strcmp(const char *str1, const char *str2)compares the string pointed to, by str1 to the string pointed to by str2.

    strcmpn


    Compares up to num characters of the C string str1 to those of the C string str2.
    This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.


  • 0

    strcmp() used to compare to words/string charecters... where  strncmp() will do the same  but it compare the string charecters on the basis of lenghtof string we give.

    ex :
    strcmp(priti, preeti) : will compare entire 2 strings.
    strncmp(priti, preeti, 3) : will compare first three charecters of both the strings.


  • 0

    strcmp compares the whole string.
    strncmp compares upto the value you specify i.e n


  • 0

    both functions are used to compare the two strings . strcmp() compares whole string where as strncmp() compare only a part of string as required by the user.


  • 0

    In C lang both the function strcmp() & strncmp() returns n integer value after comparing two strings but strcmp() compares the whole string with another string while strncmp() compares two strings upto desired (n) digits...the value of desired digit is sent during the function call..


  • 0

    strcmp() compare two strings until to reach null in any one of the string. but strncmp() compare either n number of characters or null which ever comes first


Did Not Get The Answer? 
Contact people of Talent-C Language directly from here

About Us |  Press |  Contact Us |  Careers |  Sitemap
  • Query:
  • +91 11 64000230  or
  • support@youth4work.com
© 2018 Youth4work. Terms and Privacy Policy
  • Stay Connected