gpt4 book ai didi

c - 在 for 循环 C 中返回

转载 作者:行者123 更新时间:2023-12-04 10:45:03 24 4
gpt4 key购买 nike

在下面的代码中,会返回任何东西吗?

#include <stdio.h>

int try_this (int in);

int main (void)
{
try_this (5);
}

int try_this (int in)
{
int i = 1;

for (i = 0; i < in; i = i + 2) {
return i;
}

return i;
}

既然 for 循环中有一个 return,那么代码会不会因为调用函数后什么都没有而什么都不返回?或者我会作为一个数字返回,比如 1(因为 try_this 中的声明)或 6(因为循环)?谢谢!!:)

最佳答案

当遇到第一个return 语句时,函数将返回。很容易看出该函数将启动循环,当 i=0 时它将返回 i。所以每次你调用 try_this 你都会得到 0

此外,return 0; 来自 main...

关于c - 在 for 循环 C 中返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43962487/

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