Wednesday, 1 August 2018

Strncmp usage from working



SYNOPSIS

#include <string.h>
 int strncmp(const char *s1, const char *s2, size_t n);

Description
The C library function int strncmp(const char *str1, const char *str2, size_t n) compares at most the first n bytes of str1 and str2.

RETURN VALUE
       The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively,  to  be  less  than,  to
       match, or be greater than s2.

  • strncmp will be usefully only if the both strings are not null terminated, at that time only it will be useful to stop the comparison upto n.
  • if we are using strlen to calculate lenght to give on  n, then strncmp is not useful.
  • strncmp with strlen is as good as strcmp.
  • Right way to use strncmp is to check for max allowed lenght.  we can use max size macro used to declare the string.

No comments:

Post a Comment