strncmp() and strcmp()

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

Last updated:6/22/2018 1:19:22 AM

1 Answers

Prakash nidhi Verma
Prakash nidhi Verma

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.


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