为什么c中具有returntype的函数可以在没有return语句的情况下工作?

发布于 2024-11-06 09:23:46 字数 863 浏览 1 评论 0原文

可能的重复:
为什么可以您从非 void 函数返回而不返回值而不产生编译器错误?

void main()
{
    int y=0;
    clrscr();
    printf("%d\n",Testing());
    y=Testing();
    printf("%d",y);
    getch();
}
int  Testing()
{
    int x=100;
    //return x;
}

Result 
512
4

我没有从测试函数返回任何内容,但值仍然存在?

另一个问题是,

void main()
{
    char Testing();
    int y=0;
    clrscr();
    printf("%d\n",Testing());
    if(Testing())
        printf("if--exi");
    else
        printf("else--exi");
    getch();
}
char Testing()
{
    char y;
    //return y;
}

Result

0
if--exi

如果 printf 被注释,那么结果是

else--exi

为什么会这样

Possible Duplicate:
Why can you return from a non-void function without returning a value without producing a compiler error?

void main()
{
    int y=0;
    clrscr();
    printf("%d\n",Testing());
    y=Testing();
    printf("%d",y);
    getch();
}
int  Testing()
{
    int x=100;
    //return x;
}

Result 
512
4

i am not returning any thing from the testing function still value is coming?

Another Question also

void main()
{
    char Testing();
    int y=0;
    clrscr();
    printf("%d\n",Testing());
    if(Testing())
        printf("if--exi");
    else
        printf("else--exi");
    getch();
}
char Testing()
{
    char y;
    //return y;
}

Result

0
if--exi

if printf is commented then result is

else--exi

why is it happening like that

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

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

发布评论

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

评论(6

猫七 2024-11-13 09:23:46

声明为返回到达路径但没有返回的值的函数具有未定义的行为。您只是不幸,它似乎仍在返回一个半合理的值。编译器假定在函数的所有退出中都调用了 return ,并且返回值在特定位置可用。

在第二个示例中,任何事情都可能发生,包括每次调用返回不同的值。

g++ 有一个警告来检测这个问题,并且非常值得启用。

A function declared to return a value that reaches a path without a return has undefined behavior. You just got unlucky that it appeared to still be returning a semi-plausible value. The compiler assumes that a return is called in all exits from the function and that a return value will be available at a particular location.

In your second example effectively anything can happen, including returning different values each call.

g++ has a warning to detect this problem, and it's highly worth enabling.

各自安好 2024-11-13 09:23:46

为什么你的问题同时被标记为C和C++,而标题却特指C?这两种语言在这方面有很大不同。

在 C 语言中,此类函数“起作用”是因为该语言要求它们起作用。为什么不呢?使用某种返回类型声明的函数仍然可以做一些有用的工作(除了返回任何内容)。因此,从形式的角度来看,在这样的函数中没有 return 语句是完全没有问题的。但这不是一个好的编程习惯。只要您不尝试使用返回值(该函数并未真正返回),该函数就会起作用。您正在尝试使用该值,这使得代码的行为未定义。您声称它“有效”。事实上并非如此。在这种情况下会发生什么以及为什么 - 这些问题没有答案。无论出于何种目的,您的代码的行为本质上都是随机且不可预测的。

在 C++ 语言中,“忘记”使用 return 语句返回值的函数总是立即导致未定义的行为,无论调用者是否使用该值。

Why is your question tagged C and C++ at the same time, while the title specifically refers to C? These two languages are significantly different in this regard.

In C language such functions "work" because the language requires them to work. Why not? The function that is declared with some return type might still do some useful work (besides returning anything). So, not having a return statement in such a function is perfectly fine from the formal point of view. It is not a good programming practice though. The function will work as long as you are not trying to use the returned value (which the function didn't really return). You are trying to use that value, which makes the behavior of your code undefined. You are claiming that it "works". In reality it doesn't. What happens in this case and why - these questions have no answers. For all means and purposes your code's behavior is essentially random and unpredictable.

In C++ language functions that "forget" to return a value with a return statement always immediately lead to undefined behavior, regardless of whether the value is used by the caller or not.

So要识趣 2024-11-13 09:23:46

它只是您返回的内存中的随机垃圾,因此,第二个示例中的每个调用都会产生不同的结果

Its just random junk in memory you're getting back, hence, each call in the second example produces different results

梦罢 2024-11-13 09:23:46

获取声明为不返回任何内容的函数的返回值是未定义行为。

未定义的行为意味着任何事情都是可能的,并且操作的结果不是由 C++ 标准定义的。您无法估计或近似任何程序运行的结果。

it is Undefined behavior to get the return value of function which is declared to not return anything.

Undefined behavior means that anything is possible and the outcome of the operation is not defined by the C++ standard. You cannot estimate or approximate the outcome in any of the program runs.

毁梦 2024-11-13 09:23:46

您正在导致未定义的行为,因此实际上任何输出都是可能的。

You're causing undefined behaviour, so literally any output is possible.

痴情换悲伤 2024-11-13 09:23:46

从技术上讲,您正在调用“未定义的行为”。未定义的行为表现得没有定义。如果程序的行为未定义,它可能会打印 512、4、99,甚至葛底斯堡地址的完整文本。

我不会将您所显示的结果描述为“有效”这个词的任何合理意义。

PS 如果您使用的是 GNU 编译器集合,我建议至少使用这些开关:“-Wall -Werror”。

Pps 在过去的几年里,在 ANSI 标准 C 之前的日子里,某个非优化的 Z-80 C 编译器会接受这个程序片段:

int ReturnThree() {
  3;
}

并生成与用户指定的代码相同的代码:

int ReturnThree() {
  return 3;
}

Technically, you are invoking "undefined behavior." Undefined behavior behaves, well, without definition. If the behavior of your program is undefined, it might print 512, 4, 99, or even the complete text of the Gettysburg Address.

I wouldn't describe the results you showed as 'working' in any reasonable sense of that word.

P.s. If you are using the GNU Compiler Collection, I recommend using at least these switches: "-Wall -Werror".

P.p.s. In years gone by, in the days before ANSI-standard C, a certain non-optimizing Z-80 C compiler would accept this program fragment:

int ReturnThree() {
  3;
}

and produce the same code as if the user had specified:

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