【问题标题】:strcmpi code wont compile but strcmp will? [duplicate]strcmpi 代码不会编译,但 strcmp 会吗? [复制]
【发布时间】: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 正确,但你还应该提到这个函数只能在&lt;strings.h&gt; 中声明(注意额外的 S)。 Linux 在 &lt;string.h&gt; 中确实有它,但大多数 *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部分。

标签: c cstring strcmp


【解决方案1】:

strcmpi 不是标准的 C 函数。并且编译器报告它没有函数strcmpi的声明。

您可以使用循环和函数 touppertolower 在标头 &lt;ctype.h&gt; 中贴花自己编写类似的函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 2016-11-09
    • 2013-11-28
    • 2020-10-18
    相关资源
    最近更新 更多