Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

346
Visualizações
strcasecmp() : A Non-Standard Function?

The other day I created a post over at CodeReview. One person who answered my question suggested that I refrain from using strcasecmp() because the "function is non-standard [and] this makes [my] code non-portable." This is how I used it:

int playGame()
{

    char scanned[3];
    printf("Do you wish to play tick-tack-toe?\n");
    scanf("%s", scanned);
    if(strcasecmp(scanned,"yes")==0)
        startGame();

    else
    {
        if (strcasecmp(scanned,"no")==0 || strcasecmp(scanned,"nah")==0 || strcasecmp(scanned,"naw")==0)
        {
            printf("That's too bad!/nThis program will now end.");
            return 1;
        }
        printf("Not valid input!/nThis program will now end.");
        return 1;
    }
return 0;
}

Can someone explain more in-depth and why strcasecmp() has these limitations?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

strcasecmp is not in the C or C++ standard. It's defined by POSIX.1-2001 and 4.4BSD.

If your system POSIX or BSD compliant, you'll have no problems. Otherwise, the function will be unavailable.

over 4 years ago · Santiago Trujillo Relatório

0

Short answer: As strcasecmp() is not in the C standard library, that make it non-standard.

strcasecmp() is defined in a the popular standards such as 4.4BSD, POSIX.1-2001.

The definition of case-less functions opens the door to the nit-picky details. These often involve the positive or negative result of case-less compares, not just the 0 or non-0 as used by OP. In particular:

In the POSIX locale, strcasecmp() and strncasecmp() shall behave as if the strings had been converted to lowercase and then a byte comparison performed. The results are unspecified in other locales.

The trouble with this is with upper and lower case letters that do not have a 1 to 1 mapping. Consider a local that has E, e and é but no É, yet toupper('é') -- > 'E' . Then with "as if the strings had been converted to lowercase", 'E' has 2 choices.

As a candidate portable solution consider one that round trips the letter (to upper then to lower) to cope with non 1-to-1 mappings:

int SGA_stricmp(const char *a, const char *b) {
  int ca, cb;
  do {
     ca = * (unsigned char *)a;
     cb = * (unsigned char *)b;
     ca = tolower(toupper(ca));
     cb = tolower(toupper(cb));
     a++;
     b++;
   } while (ca == cb && ca != '\0');
   return ca - cb;
}

If you do not want to round-trip the values use:

     ca = tolower(ca);
     cb = tolower(cb);

Detail: toupper() and tolower() only defined for int in the range of unsigned char and EOF. * (unsigned char *)a used as *a may have negative values.

over 4 years ago · Santiago Trujillo Relatório

0

"The function is non-standard" means, that the function declaration and contract aren't specified in The C International Standard.

"This makes code non-portable" means, that implementations aren't required to implement strcasecmp(), and therefore your code is not fully standard-compliant and not guaranteed to be compiled by strictly standard-conforming compilers.

strcasecmp() is itself a part of the POSIX.1-2001 and 4.4BSD specifications (link).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda