strncmp() and strcmp()

Total Post:113

Points:791
Java 
 183  View(s)
Ratings:
Rate this:

What is the Difference between strncmp() and strcmp() ?

  1. Post:140

    Points:984
    Re: strncmp() and strcmp()

    strcmp and strncmp :

    null-character of string comes in strcmp for compares both the strings wheres 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. if both of the strings passed in the argument with strcmp function is not terminated its create a  problem by null char ,then comparison of characters continues till the system crashes. But with strncmp function we can limit the comparison with num paramet. 

    strncmp():  in C++ This function takes two strings and a number num as arguments and compare at most first num bytes of both the strings. 

    int strncmp(const char *str1, const char *str2, size_t count); 
    str1 and str2: C string to be compared.
    count: Maximum number of characters to compare.
     size_t is an unsigned integral type.

    strcmp():  it's <string.h> header file which is a built-in library function and declared in This function takes compare these two strings lexicographically and it's two strings as arguments.This process will be continued until a characters are unequal.

    Syntax:

    int strcmp(const char *leftStr, const char *rightStr ); 

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!