没有头文件的 C 函数
这应该是非常微不足道的。我正在运行一个非常基本的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你用的是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.不像之前的答案那么优雅,这是完成此任务的另一种方法
Not as elegant as the earlier answers, another way to get this done
不知道什么编译器和lib。您使用的版本,所有结论都只是“可能性”。因此,最合理的做法是
stdio.h
已经包含stdlib.h
或string.h
Without knowing what compiler and lib. version you use, all conclusion are only 'possibility'. So the most reasonable thing that
stdio.h
already includesstdlib.h
orstring.h
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 usestrcmp
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.