lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Em seg., 16 de nov. de 2020 às 12:41, Andre Leiradella <andre@leiradella.com> escreveu:


On 16/11/20 15:35, Ranier Vilela wrote:

False.
Strncmp always is faster.
Once strcmp, it will go to the end of the string, always.
Not false, since strcmp only goes as far as the first NUL is found in one of the strings.
Maybe you can then explain why they implemented strncmp, if strcmp solves everything and is as fast and better than strncmp?

/* strncmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
  int n;
  puts ("Looking for R2 astromech droids...");
  for (n=0 ; n<3 ; n++)
    if (strncmp (str[n],"R2xx",2) == 0)
    {
      printf ("found %s\n",str[n]);
    }
  return 0;
}
Why would they take the trouble?

Ranier Vilela