当模块化 C 代码时,在一个函数内有没有办法循环到另一个函数(即到我的 main() c 文件)?

dys*_*key 3 c modularity

我在大学的第一个学期学习 C 编程。我们已经获得了模块化代码的介绍,我的问题的基本要点是我创建了一个提示用户继续或退出程序的函数。显然,在此之前我还有其他一些函数,我在 main() 中调用它们。请记住,这些函数位于单独的 C 文件中。如果用户输入“Y”(即从头开始启动程序),我希望循环回到 main(),但鉴于我的初学者知识,我很难弄清楚如何去做。任何帮助/指导将不胜感激!

int continueOrExit() {
    char response; 
    char upperResponse;
    printf("Do you wish to continue the program? Y/N");
    scanf("%c", &response);
    upperResponse = toupper(response);

    while(upperResponse == 'Y')
        main(); // this is the bit which I struggle with

    .....
    }
Run Code Online (Sandbox Code Playgroud)

Bor*_*itz 5

你不应该 运行您的main()从从已调用的函数内再次main()。好吧,你可以;递归调用是可能的,但在这种情况下你绝对不想这样做。

你想要做什么是你continueOrExit()回到真正的当用户选择继续,并把你的选择的很大一部分main()身体在一个循环:

do
{
    /* your code here */
} while (continueOrExit());
Run Code Online (Sandbox Code Playgroud)