温馨提示×

c语言中如何返回主界面

小亿
111
2024-06-06 17:19:21
栏目: 编程语言

在C语言中,通常是通过使用循环来实现程序返回主界面的功能。可以在主函数中设置一个循环,让程序在完成一次操作后返回到主界面,等待用户输入下一个操作。例如:

#include <stdio.h>

int main() {
    char choice;

    do {
        printf("Main Menu\n");
        printf("1. Option 1\n");
        printf("2. Option 2\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf(" %c", &choice);

        switch(choice) {
            case '1':
                // Code for option 1
                printf("Option 1 selected\n");
                break;
            case '2':
                // Code for option 2
                printf("Option 2 selected\n");
                break;
            case '3':
                printf("Exiting program\n");
                break;
            default:
                printf("Invalid choice\n");
        }
    } while(choice != '3');

    return 0;
}

在上面的例子中,程序会一直循环显示主菜单,直到用户选择退出程序(输入3)。用户可以选择不同的选项,执行对应的操作,然后再次回到主菜单。当用户选择退出程序时,程序会结束并返回主函数的结束语句,从而返回到主界面。

0