【发布时间】:2015-12-24 12:39:28
【问题描述】:
我现在正在学习 C 编程语言并尝试理解其中的功能。 下面的代码在 Visual Studio 中没有给出错误,并返回发送给该函数的第一个参数。 我认为它应该给出一个错误,但它没有。
给定的代码如何工作以及如何返回代码中的第一个参数 3?
#include <stdio.h>
#include <stdlib.h>
int hesapla(int x, int y);
void f();
int main()
{
int a, b, sonuyc;
int s;
printf("\n");
printf("sonuc = %d", hesapla(3, 10));
system("pause");
return 0;
}
int hesapla( int x, int y) {
if (x > y )
return y;
if (x == y)
return y + 1;
}
谢谢你..
【问题讨论】:
-
I think it should give an error but it does not....为什么会这样? -
x < y时返回值未确定。 -
你的函数永远不会返回
x。你需要添加一个条件来做到这一点。 -
我认为这是未定义的行为。不需要错误消息。如果不正确使用
return,编译器无法始终确定是否存在导致函数脱落的可能代码路径。 -
当我们向函数发送大于第二个参数的第一个参数时,函数中的 if 条件将不成立,因此 return 语句将无法访问,因为我认为它应该给出错误。如何决定在那种情况下返回哪个值?
标签: c function return-value