函数内的函数

发布于 2024-10-21 22:06:40 字数 226 浏览 4 评论 0原文

function1()
    {
        statement1;
        statement2;
        function2()
        {
                statement3;
                statement3;
        }
    } 

为什么控制不进入 function2,即使两个函数的返回类型相同

function1()
    {
        statement1;
        statement2;
        function2()
        {
                statement3;
                statement3;
        }
    } 

why does control not enter function2, even though return type of both the functions are same

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

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

发布评论

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

评论(3

走过海棠暮 2024-10-28 22:06:40

如果你想进入function2,你必须调用它。事实上,将它放入另一个函数中并不意味着它将被执行,但仍会被声明和定义。你必须显式地调用它

function1()
    {
    statement1;
    statement2;
    function2()
    {
            statement3;
            statement3;
    }
 function2();

} 

,事实上 Std C 不允许这样做。但这仍然取决于您的编译器,因此如果您出于某种目的这样做,请咨询您的编译器,否则只需将 function2 声明从 function1 的块中取出

If you want to enter function2 you have to call it. The fact that you put it inside another function doesn't mean it's going to be executed, but yet declared and defined. You have to explicitly call it

function1()
    {
    statement1;
    statement2;
    function2()
    {
            statement3;
            statement3;
    }
 function2();

} 

And indeed Std C doesn't allow this. But it still depends on your compiler, so if you're doing this on some purpose, check with your compiler otherwise just pull the function2 declaration out of the function1's block

野味少女 2024-10-28 22:06:40

这不是标准定义的合法 C。它甚至可以编译吗?

更新:假设GCC,CoolStraw的答案是正确的。

This is not legal C as defined by the standard. Does it even compile?

Update: Assuming GCC, CoolStraw's answer is correct.

黎歌 2024-10-28 22:06:40

最初问题的答案是,正如 CoolStraw 所说,定义嵌套函数是不够的,您必须在希望执行它的时候显式调用它。在此之前必须贴标。它与包含函数是否具有相同的类型无关。

正如 Mat 所说,C 标准不允许在函数内定义函数。它是 gcc 扩展,IBM 的 XLC 编译器也支持它。

自大约 1970 年最初的 K&RC 以来,函数的结果类型默认为 int,并且是迄今为止所有 C 标准的一部分。这不是伪代码或 gcc 额外内容。现在人们普遍不鼓励这样做,因为它隐藏了错误。例如,如果您忘记“#include”并使用返回指针的字符串函数,它将默认返回 int。如果这些类型大小相同,您就可以摆脱它,但在具有 32 位整数的 64 位模式下,丢弃指针的上半部分并用下半部分的符号扩展替换它是一个令人讨厌的错误。

许多编译器会发出某种消息警告您省略了函数的类型,许多编译器还会警告您正在调用非 void 函数而不使用结果。两者都是合法的,两者都可能正是您想要的,但两者都可能是错误的。大多数编译器还可以警告不符合标准的扩展,例如 gcc 的嵌套函数。如果您关心可移植性,请启用类似的警告。

The answer to the original question is as CoolStraw said that it isn't enough to define a nested function, you have to explicitly call it at the point you want it executed. It must be decalred before that. It has nothing to do with whether it has the same type as the containing function.

As Mat said, defining a function inside a function is not allowed by the C standard. It is a gcc extension also supported by IBM's XLC compiler.

The result type of a function has defaulted to int since the original K&R C circa 1970, and is part of all C standards so far. This is not pseudocode or a gcc extra. It's generally discouraged now because it hides mistakes; for example, if you forget "#include " and use a string function returning a pointer, it will default to returning an int. If those types are the same size, you'll get away with it, but in 64 bit mode with 32 bit ints, discarding the upper half of a pointer and replacing it with the sign extension of the lower half is a nasty bug.

Many compilers can give some kind of message warning that you've omitted the type of a function, and many will also warn that you're calling a non-void function without using the result. Both are legal, both may be exactly what you intended, but both may be errors. And most compilers can also warn about non-standard compliant extensions like gcc's nested functions. If you care about portability, enable warnings like those.

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