gpt4 book ai didi

c - 函数没有 return 语句返回值

转载 作者:行者123 更新时间:2023-11-30 16:27:11 24 4
gpt4 key购买 nike

为什么下面的代码有正确的输出? int GGT 没有 return 语句,但是代码仍然可以工作?没有设置全局变量。

#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;
}
}
}

最佳答案

至少对于x86,该函数的返回值应该位于eax寄存器中。那里的任何内容都将被调用者视为返回值。

因为eax用作返回寄存器,所以它经常被被调用者用作“临时”寄存器,因为它不需要被保留。这意味着它很可能被用作任何局部变量。因为最后两者相等,所以 eax 中保留正确值的可能性更大。

关于c - 函数没有 return 语句返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52818944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com