函数内的函数
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想进入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
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
这不是标准定义的合法 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.
最初问题的答案是,正如 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.