【发布时间】:2014-08-27 17:58:08
【问题描述】:
我有一个问题,为什么当我使用 strcmpi 时我的代码无法编译。我用 strcmp 测试了相同的代码并且有效。不知道为什么这不起作用。
这是我得到的编译错误:
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;
}
【问题讨论】:
-
strcmpi不是标准函数。在 Linux 上,有strcasecmp,它会进行不区分大小写的比较。 -
@MOehm 正确,但你还应该提到这个函数只能在
<strings.h>中声明(注意额外的 S)。 Linux 在<string.h>中确实有它,但大多数 *BSD 没有。 -
@Zack:好点;我不知道。 (但我相信 OP 会在知道名称后查找函数及其声明位置。)
-
嗨,谢谢大家的帮助。所以我用strcasecmp改变了strcmpi。这似乎可以编译,但是一旦我编译它就会收到警告。 “strcmpi_test.c:在函数'main'中:strcmpi_test.c:15:警告:函数'strcasecmp'的隐式声明strcmpi_test.c:29:2:警告:文件末尾没有换行符”
-
仔细阅读the documentation for
strcasecmp,特别注意RETURN VALUE部分。