简体   繁体   English

C函数没有头文件

[英]C functions without header files

This should be very trivial. 这应该是非常微不足道的。 I was running through a very basic C program for comparing strings: 我正在运行一个非常基本的C程序来比较字符串:

#include <stdio.h>  
int strcmp(char *s, char *t);
int main()
{
    printf("Returned: %d\n", strcmp("abc", "adf"));
    return 0;
}

int strcmp(char *s, char *t)
{
    printf("Blah\n");
    while (*s++ == *t++)
    {
        if (*s == '\0')
            return 0;
    }
    return *s - *t;
}

So I've basically implemented my own version of the strcmp function already present in string.h. 所以我基本上实现了我自己的string.h中已经存在的strcmp函数版本。 When I run the above code, I only see return values of 0, 1, or -1 (at least for my small set of test cases) instead of the actual expected results. 当我运行上面的代码时,我只看到返回值0,1或-1(至少对于我的一小组测试用例)而不是实际的预期结果。 Now I do realize that this is because the code doesn't go to my implemented version of strcmp, but instead uses the string.h version of the function, but I'm confused as to why this is the case even when I haven't included the appropriate header file. 现在我意识到这是因为代码不是我的strcmp的实现版本,而是使用函数的string.h版本,但我很困惑,为什么会出现这种情况,即使我没有' t包括适当的头文件。

Also, seeing how it does use the header file version, shouldn't I be getting a 'multiple implementations' error (or something along those lines) when compiling the code? 另外,看看它是如何使用头文件版本的,在编译代码时,我不应该得到“多个实现”错误(或者这些行中的某些内容)吗?

You're using gcc, right? 你正在使用gcc,对吧? gcc implements some functions as built-ins in the compiler and it seems that strcmp is one of those . gcc在编译器中实现了一些内置函数,看起来strcmp就是其中之一 Try compiling your file with the -fno-builtin switch. 尝试使用-fno-builtin开关编译文件。

Header files just tell the compiler that certain symbols, macros, and types exist. 头文件只告诉编译器存在某些符号,宏和类型。 Including or not including a header file won't have any effect on where functions come from, that's the linker's job. 包含或不包含头文件不会对函数的来源产生任何影响,这是链接器的工作。 If gcc was pulling strcmp out of libc then you probably would see a warning. 如果gcc将strcmp从libc中拉出来,那么你可能会看到一个警告。

Not as elegant as the earlier answers, another way to get this done 不像以前的答案那样优雅,另一种方法可以完成

#include <stdio.h>  
static int strcmp(char *s, char *t); /* static makes it bind to file local sym */
int main()
{
    printf("Returned: %d\n", strcmp("abc", "adf"));
    return 0;
}

int strcmp(char *s, char *t)
{
    printf("Blah\n");
    while (*s++ == *t++)
    {
        if (*s == '\0')
            return 0;
    }
    return *s - *t;
}

Without knowing what compiler and lib. 不知道什么编译器和lib。 version you use, all conclusion are only 'possibility'. 您使用的版本,所有结论都只是“可能性”。 So the most reasonable thing that stdio.h already includes stdlib.h or string.h 所以最合理的是stdio.h已经包含了stdlib.hstring.h

strcmp is the name of a standard library function. strcmp是标准库函数的名称。 As such you are only permitted to declare the function (although you must use a correct declaration); 因此,您只能声明该函数(尽管您必须使用正确的声明); you are not permitted to provide another definition for it. 您不得为其提供其他定义。 The implementation can assume that whenever you use strcmp you are referring to the standard library function even if you haven't used the correct #include for it. 实现可以假设,无论何时使用strcmp ,即使您没有使用正确的#include ,也指的是标准库函数。

If you want to provide an alternative strcmp then you should give it an alternative name. 如果你想提供一个替代strcmp那么你应该给它一个替代名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM