gpt4 book ai didi

c - 荒谬的汇编

转载 作者:太空狗 更新时间:2023-10-29 15:53:16 24 4
gpt4 key购买 nike

这是一个基本的 C 代码,根据我的说法,它应该抛出三个错误(函数未定义、函数不返回任何内容、函数参数丢失)。但令我惊讶的是它没有抛出任何东西,它编译并给出了一些垃圾结果:

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[])
{
int a=f1();
printf("a %d\n",a);
system("PAUSE");
return 0;
}


f1(int *t)
{
printf("t %d", t);
}

PS: 我在 Windows 上使用 gcc 编译器。

最佳答案

在 C 中,如果未声明函数,则假定返回 int 并继续编译(顺便说一句 this can lead to nasty bugs)。如果函数声明时没有类型(如 f1() 在您的代码中,则假定返回 int。不从非 void 函数返回值(如在你的代码中)是未定义的行为。

因此,您提到的所有点都不是导致编译错误所必需的。不需要未定义的行为来阻止您的程序运行 - 该程序可能会运行,甚至可能会产生漂亮的结果。

关于c - 荒谬的汇编,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7062302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com