不包括;

发布于 2024-10-11 08:18:25 字数 148 浏览 3 评论 0原文

下面给定的程序是否可以在不包含 的情况下工作?为什么这有效?

int main()
{
    printf("integra");
    return 0;
}

below given program is working without including <stdio.h>? Why does this work?

int main()
{
    printf("integra");
    return 0;
}

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

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

发布评论

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

评论(6

画骨成沙 2024-10-18 08:18:25

printf() 的定义位于 libc.so 中,即使您不包含头文件,动态链接器也会处理它。在编译时, printf() 将是一个未定义的符号,并且它假设稍后可以在 libc 中找到定义。头文件只会给出原型类型并抑制编译器(警告),表明原型的定义存在于 glibc 中。所以基本上,包含头文件只是为了确保定义在我们的库中可用,以帮助开发人员。

Definition of printf() is there in libc.so and the dynamic linker will take care of it even if you don't include the header file. During compile time, printf() will be an undefined symbol and it assumes that it may find the definition later on in libc. The header file will just give the proto-type and suppress the compiler(warnings) stating that the definition of the prototype is present in glibc. So basically, the header files are included just to make sure that the definitions are available in our libraries, to help the developer.

§对你不离不弃 2024-10-18 08:18:25

在旧标准中,未声明的函数采用 int 参数和返回值。您的 char* 与 int 具有相同的大小(32 位),因此一切正常。

只是不要这样做。

in older standard, undeclared function assume int argument and return value. Your char* have same size (32-bit) as int, so everything work.

Just don't do it.

情魔剑神 2024-10-18 08:18:25

printf() 仅在 libc.so 中定义

动态链接器将解析 libc 中的符号 printf(),因为您尚未包含它

libc 是每个程序的 gcc 中的默认值

printf() is only defined in libc.so

The dynamic linker will resolve the symbol printf() in libc since you have not included it

libc is default in gcc for every program

水水月牙 2024-10-18 08:18:25

正如 Abi 指出的,您的代码在不包含 stdio.h 的情况下构建成功,因为链接器默认使用系统 std 库来处理未定义的符号 (printf)。 GCC 通常会警告您此类情况。

让 test.c 为:

1: int main()
2: {
3:    printf("test\n");
4:    return 0;
5: }

在 Mac OSX 上使用 GCC 4.2.1 构建 test.c:

$ gcc test.c
test.c: In function ‘main’:
test.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
$ ./a.out
test

您可以通过指定 GCC 链接器选项 -nostdlib (或 -nodefaultlibs) 以及 -lgcc (按照 GCC 手册的建议):

$ gcc -nostdlib -lgcc test.c
test.c: In function ‘main’:
test.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
Undefined symbols:
  "_puts", referenced from:
      _main in cc3bvzuM.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
$

As Abi pointed out, your code builds successfully without including stdio.h because the linker is defaulting to the system std library for the undefined symbol (printf). GCC would normally warn you of such cases.

Let test.c be:

1: int main()
2: {
3:    printf("test\n");
4:    return 0;
5: }

Building test.c with GCC 4.2.1 on Mac OSX:

$ gcc test.c
test.c: In function ‘main’:
test.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
$ ./a.out
test

You can disable this default linking by specifying a GCC linker option -nostdlib (or -nodefaultlibs) along with -lgcc (as the GCC manual recommends):

$ gcc -nostdlib -lgcc test.c
test.c: In function ‘main’:
test.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
Undefined symbols:
  "_puts", referenced from:
      _main in cc3bvzuM.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
$
叹梦 2024-10-18 08:18:25

一些(旧的?)编译器在调用函数之前不需要原型。

Some (old?) compilers do not require prototypes before calling functions.

予囚 2024-10-18 08:18:25

当您使用尚未声明的函数时,编译器将假定该函数返回 int 并采用未指定但固定数量的参数。

如果此假设与函数的定义匹配,并且您提供的参数也与函数期望接收的参数匹配(以默认参数提升为模),那么一切都很好。
如果假设不正确(例如 printf,它是一个可变参数函数),或者当参数不匹配时,结果是未定义的。未定义行为的令人讨厌的事情之一是它看起来可以按预期工作。

When you use a function that has not been declared, then the compiler will assume this function returns an int and takes an unspecified, but fixed, number of arguments.

If this assumption matches with the definition of the function, and if the arguments you provided also match (modulo the default argument promotions) the parameters that the function expects to receive, then everything is well.
If the assumption is incorrect (like for printf, which is a variadic function), or when the arguments don't match, the results are undefined. One of the nasty things of undefined behaviour is that it can appear to work as expected.

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