此函数如何在不应该返回值时返回值?
How does this function return a value when it shouldn't?
这个函数返回一个不应该返回的值。
#include<iostream>
using namespace std;
int foo(int a,int b)
{
if(a>b)
return a;
else if(a<b)
return b;
}
int main()
{
int x=7,y=7;
cout<<foo(x,y);
return 0;
}
输出为:
7
而且它只在GCC编译器上产生正确的输出(我使用Dev c++)。Turbo C产生了垃圾值。有人能解释一下这是怎么发生的吗?
在所有程序控制路径上不返回值的行为为undefined。
编译器可以做任何事情。
你的编译器没有警告你吗?相关文章:
- 为什么模板类中的对象不能返回值
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 方法错误"not all control paths return a value"和方法不返回值
- 程序不向函数返回值
- C++ 特征图3.5,特征图不使用命名返回值优化?
- 显示C++输出而不返回值
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 从 C++ 函数与 Python 函数返回的不一致值用于偏斜正态分布
- 难以理解为什么返回值不会更新值
- 为什么函数的返回值不是符号名称的一部分
- 返回值不变的函数
- 绑定的返回值不是int
- 此函数如何在不应该返回值时返回值?
- 使用c++数学比较函数返回值不起作用
- 函数的返回值不能被识别为左值
- 可以声明一个c++函数,使其返回值不能被忽略吗?
- 动态多态内存容器-返回值不正确
- 赋值运算符在不应该返回"empty"实例时返回?
- C++:函数返回指针不应该是常量的任何原因?