没有头文件的 C 函数

发布于 2024-11-06 05:35:20 字数 614 浏览 1 评论 0原文

这应该是非常微不足道的。我正在运行一个非常基本的 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;
}

所以我基本上实现了 string.h 中已经存在的我自己版本的 strcmp 函数。当我运行上面的代码时,我只看到返回值 0、1 或 -1(至少对于我的一小组测试用例而言),而不是实际的预期结果。现在我确实意识到这是因为代码没有转到我实现的 strcmp 版本,而是使用该函数的 string.h 版本,但我很困惑为什么会出现这种情况,即使我没有t 包含适当的头文件。

另外,看看它如何使用头文件版本,我在编译代码时不应该收到“多个实现”错误(或类似的错误)吗?

This should be very trivial. I was running through a very basic C program for comparing strings:

#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. 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. 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愛上了 2024-11-13 05:35:20

你用的是gcc,对吧? gcc 将一些函数实现为编译器中的内置函数,并且似乎 strcmp 就是其中之一。尝试使用 -fno-builtin 开关编译您的文件。

头文件只是告诉编译器某些符号、宏和类型存在。包含或不包含头文件不会对函数的来源产生任何影响,这是链接器的工作。如果 gcc 将 strcmp 从 libc 中拉出,那么您可能会看到一条警告。

You're using gcc, right? gcc implements some functions as built-ins in the compiler and it seems that strcmp is one of those. Try compiling your file with the -fno-builtin switch.

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.

空城缀染半城烟沙 2024-11-13 05:35:20

不像之前的答案那么优雅,这是完成此任务的另一种方法

#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;
}

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;
}
你是我的挚爱i 2024-11-13 05:35:20

不知道什么编译器和lib。您使用的版本,所有结论都只是“可能性”。因此,最合理的做法是 stdio.h 已经包含 stdlib.hstring.h

Without knowing what compiler and lib. version you use, all conclusion are only 'possibility'. So the most reasonable thing that stdio.h already includes stdlib.h or string.h

白鸥掠海 2024-11-13 05:35:20

strcmp 是标准库函数的名称。因此,您只能声明该函数(尽管您必须使用正确的声明);您不得为其提供其他定义。该实现可以假设每当您使用 strcmp 时,您都会引用标准库函数,即使您没有使用正确的 #include 也是如此。

如果您想提供替代的 strcmp 那么您应该给它一个替代名称。

strcmp is the name of a standard library function. 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.

If you want to provide an alternative strcmp then you should give it an alternative name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文