我认为这个程序的输出将是4321。即模1234 = 4,然后除以10得到123,所以模123 = 3......完成后应该是4321。但输出是1234。有人可以解释一下这是怎么发生的吗?非常感谢,节日快乐。
void printnumber(int n) { //function declaration
if (n < 0) {
putchar('-');
printnumber(-n); //recursive call
} else {
if (n >= 10) {
printnumber(n / 10); //second recursive call
}
putchar('0' + (n % 10));
}
}
int main() {
int n = 1234;
printnumber(n);
putchar('\n');
return 0;
}
最佳答案
First call to function, input 1234
Second call to fucntion, input 123
Third call to function, input 12
Fourth call to function, input 1
print 1 and return
print 2 and return
print 3 and return
print 4 and return
Output: 1234
关于c - 为什么 C 中的递归会向后打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27662595/