2

I have a question on why my code wont compile when i use strcmpi. I tested this same code with strcmp and that worked. Not sure why this does not work.

here is the compile error i get :

gcc -std=c99 strcmpi_test.c -o strcmpi_test
strcmpi_test.c: In function 'main':
strcmpi_test.c:15: warning: implicit declaration of function 'strcmpi'
strcmpi_test.c:30:2: warning: no newline at end of file
/tmp/cceKXLcn.o: In function `main':
strcmpi_test.c:(.text+0x50): undefined reference to `strcmpi'
collect2: ld returned 1 exit status



#include <stdio.h>
#include <string.h>


int main()
{

    char name[10]; 

    char name2[10] = "bob";

    printf("what is your name : ");
    fgets(name,10,stdin);

    if(strcmpi(name,name2) == 1)
    {

        printf("name == %s name2 == %s your names are the same\n",name,name2);

    } else {

        printf("name == %s name2 == %s your names are NOT the same\n",name,name2);


    }


    return 0;

}
Max Powers
  • 1,119
  • 4
  • 23
  • 54
  • 5
    `strcmpi` is not a standard function. On Linux, there's `strcasecmp`, which does case-insensitive comparison. – M Oehm Aug 27 '14 at 17:59
  • 1
    @MOehm Correct, but you should also mention that this function may only be declared in `` (note the extra S). Linux does have it in `` but most of the \*BSDs don't. – zwol Aug 27 '14 at 18:10
  • 1
    @Zack: Good point; I didn't know that. (But I trust the OP to look up the function and where it is declared, once the name is known.) – M Oehm Aug 27 '14 at 18:33
  • Hi Thanks everyone for your help. So I have changed the strcmpi with strcasecmp. This seems to compile however I get a warning once I compile it. "strcmpi_test.c: In function 'main': strcmpi_test.c:15: warning: implicit declaration of function 'strcasecmp' strcmpi_test.c:29:2: warning: no newline at end of file" – Max Powers Aug 27 '14 at 18:43
  • Also one other thing is the logic does not seem to work here either. i type the same names of bob and compare with bob and that is going to the part of the code that states the names are not the same. – Max Powers Aug 27 '14 at 18:45
  • here is the output: type your name : bob name == bob name2 == bob your names are NOT the same – Max Powers Aug 27 '14 at 18:46
  • 1
    Read [the documentation for `strcasecmp`](http://linux.die.net/man/3/strcasecmp) carefully, paying especial attention to the **`RETURN VALUE`** section. – zwol Aug 27 '14 at 18:56
  • Thanks for the documentation. I am ware that a int will be returned. Per the documentation it says this : The strcasecmp() function compares the two strings s1 and s2, ignoring the case of the characters. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. – Max Powers Aug 27 '14 at 19:24
  • So why check if the result `==1` when it also may be, per your own quote, `==42` or even the weird number `==7912`? (Big Fat Hint: "to be *greater than*" .) (I also just realized you are testing for the entire wrong *case*, hint hint) – Jongware Aug 27 '14 at 20:40

1 Answers1

1

strcmpi is not a standard C function. And the compiler reports that it has no the declaration of function strcmpi.

You can write a similar function yourself using a loop and functions toupper or tolower decalred in header <ctype.h>

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335