【发布时间】:2011-06-06 09:39:54
【问题描述】:
为什么下面的代码有正确的输出? int GGT 没有返回语句,但代码确实有效吗?没有设置全局变量。
#include <stdio.h>
#include <stdlib.h>
int GGT(int, int);
void main() {
int x1, x2;
printf("Bitte geben Sie zwei Zahlen ein: \n");
scanf("%d", &x1);
scanf("%d", &x2);
printf("GGT ist: %d\n", GGT(x1, x2));
system("Pause");
}
int GGT(int x1, int x2) {
while(x1 != x2) {
if(x1 > x2) {
/*return*/ x1 = x1 - x2;
}
else {
/*return*/ x2 = x2 - x1;
}
}
}
【问题讨论】:
-
在编译器上调高警告级别,您应该会收到一条消息...
-
我收到一条警告消息,但我很想知道为什么它会起作用,编译器是否设置了返回值,如果没有的话?
-
请注意,
-Wreturn-type(以及不太具体的-Wall)会为gcc捕获此错误。
标签: c return-value