【问题标题】:Ignoring the stdio.h file in a c file [duplicate]忽略c文件中的stdio.h文件[重复]
【发布时间】:2012-11-28 10:15:29
【问题描述】:

可能重复:
Why #include <stdio.h> is *not* required to use printf()?

我在下面给出的代码中遇到了问题。

int main()
{
printf("\nHello Stack Overflow\n\n") ;
return 0 ;
}

在上面提到的代码中,我留下了“#include”。 如果我编译并执行这段代码,输出会按预期打印。但是“#include”是C程序中最重要的东西,我忽略了它,编译仍然没有任何错误但有警告。

为什么会这样?

【问题讨论】:

标签: c linux gcc


【解决方案1】:

在 C 中,未声明的函数被隐式视为返回 int 并采用 int 参数。

这是不好的做法,会咬你。例如,如果您想打印与 int 大小不同的数据,例如 doublefloat

【讨论】:

  • 你解开了我的困惑。我之前写了一个返回 void* 的函数,但我忘记声明它。它在 32 位机器上运行良好。但是在 64 位上它总是返回 32 位。谢谢
【解决方案2】:

来自 man gcc

   -nostdlib
       Do not use the standard system startup files or libraries when linking.  No startup files and only the libraries you specify will be
       passed to the linker.  The compiler may generate calls to "memcmp", "memset", "memcpy" and "memmove".  These entries are usually
       resolved by entries in libc.  These entry points should be supplied through some other mechanism when this option is specified.

【讨论】:

  • 这可能是 OP 的问题,但我不这么认为。如果一个人理解包含头文件并不提供函数的实现,只提供它的原型,那么相反,省略必要的头文件并不会删除函数的实现,只删除它的原型也就不足为奇了。我认为 OP 的问题是为什么他没有收到警告。
  • OP 提到 编译完成时没有任何错误但有警告。 另请阅读 -nostdlib 上的文档
【解决方案3】:

printf 符号在编译时是未知的,但 libc 中的 implicit 链接到二进制文件中......这就是它的工作原理。

【讨论】:

    猜你喜欢
    • 2020-02-17
    • 1970-01-01
    • 2021-08-15
    • 2019-08-20
    • 2012-01-18
    • 2015-12-30
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    相关资源
    最近更新 更多