gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:00:36 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被用作返回寄存器,它经常被被调用者用作“scratch”寄存器,因为它不需要被保存。这意味着它很有可能被用作任何局部变量。因为它们最后都相等,所以更有可能将正确的值留在 eax 中。

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

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