C 为goto函数创建外部标签以跨源文件工作

C 为goto函数创建外部标签以跨源文件工作,c,codeblocks,C,Codeblocks,嘿,伙计们,我有一个简短的问题,可能有一个更简短的答案。我做了一些挖掘,似乎找不到答案。我最近重新开始编程(代码中的C:Blocks/GNU GCC编译器),我一辈子都记不起如何使用“goto”语句跨模块返回到标签。这是到目前为止我的代码 另外,所有文件都在同一个项目中,在同一个源代码文件夹中。我将扩展这个程序,我首先创建代码将在其中工作的框架,因为其余的都是复制和粘贴,只需更改问题和答案 main.c #包括 #包括 无效第二(无效); 无效第三(无效); 整数问题=0; int Ans=0

嘿,伙计们,我有一个简短的问题,可能有一个更简短的答案。我做了一些挖掘,似乎找不到答案。我最近重新开始编程(代码中的C:Blocks/GNU GCC编译器),我一辈子都记不起如何使用“goto”语句跨模块返回到标签。这是到目前为止我的代码

另外,所有文件都在同一个项目中,在同一个源代码文件夹中。我将扩展这个程序,我首先创建代码将在其中工作的框架,因为其余的都是复制和粘贴,只需更改问题和答案


main.c
#包括
#包括
无效第二(无效);
无效第三(无效);
整数问题=0;
int Ans=0;
int main()
{
系统(“彩色FC”);
printf(“此程序正在测试链接模块的方法。\n”);
printf(“为此,选择的两个主题是数学和科学。\n\n”);
printf(“您想选择数学还是科学?\n”);
printf(“1.Science\n”);
printf(“2.数学\n\n”);
//响应第一个输入
scanf(“%d”和问题);
如果(问题==1)
{
第二个();
}
如果(问题==2)
{
第三();
}
//当需要第一次输入且模块已完成时。
//这就是我希望返回的地方。
转到结束;
//结束节目
结束语:
{
系统(“cls”);
printf(“程序现在将退出。\n”);
系统(“暂停”);
}
返回0;
}

第二,c
#包括
#包括
#包括
第二个无效(无效)
{
系统(“暂停”);
系统(“cls”);
系统(“颜色E5”);
printf(“这是科学模块\n”);
系统(“暂停”);
返回main();
//我想回到标签:结束;从这里开始
}

第三.c
#包括
#包括
#包括
无效第三(无效)
{
系统(“暂停”);
系统(“cls”);
系统(“彩色A4”);
printf(“这是数学模块。\n”);
系统(“暂停”);
返回main();
//我想回到标签:结束;从这里开始
}

如果有人能告诉我需要做什么,那将是一大堆感谢。

你不能转到你职能范围之外的标签。此外,您的程序递归地调用main(),这几乎肯定是一件坏事


编辑:如果要返回到标记的点,只需返回。

goto
只能在函数中传输控制。不存在函数间或模块间
goto
。有
setjmp/longjmp
,但它是一个库级功能,具有明显不同的语义。那么,有没有其他方法可以转到程序的特定部分,就像返回到主源代码一样,当我返回到主源代码时,直接执行结束程序的操作???您可能对
退出
库函数.tl感兴趣;博士,但这听起来像是一个XY问题和一个非常糟糕的设计。简单地说:不要“如果有人能透露一些我需要做的事情,那将是感谢堆。”-没有冒犯,但最好是重新访问结构化和模块化编程的基础知识。我实际上可能只是创建另一个源文件作为结尾,并将其发送到那里,这可能会容易得多,但我仍然希望看到一个实现此私有消息的另一种方法的示例。如果您想在C中快速调用
main
,则该示例是合法的。不这样做不会使代码变得更好。见鬼,
goto
cross-module在这种情况下非常简单,递归调用main意味着代码不能满足询问者的要求。
#include <stdio.h>
#include <stdlib.h>


void second(void);
void third(void);


int question = 0;
int Ans = 0;

int main()
{
    system("COLOR FC");
    printf("This program is testing a method of linking modules.\n");
    printf("For this purpose the two topics chosen are maths and science.\n\n");
    printf("Would you like to choose maths or science?\n");
    printf("1. Science\n");
    printf("2. Maths\n\n");

//Responding to the first input

    scanf("%d",&question);
    if(question==1)
    {
         second();
    }
    if(question==2)
    {
         third();
    }
  //When the first input has been desired and the module has been completed.
  //This is where i wish to return.
goto Ending;

  //Ending the Program
Ending:
{
    system("cls"); 
    printf("The program will now exit.\n");
    system("pause");
}


   return 0;
   }
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


void second(void)
{
    system("pause");
    system("cls");
    system("COLOR E5");
    printf("Here is the Science Module\n");



    system("pause");
    return main();
    //I WANT TO RETURN TO THE LABEL:ENDING; FROM HERE
 }
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


void third(void)
{
     system("pause");
     system("cls");
     system("COLOR A4");
     printf("Here is the Maths Module.\n");

     system("pause");
     return main();
     //I WANT TO RETURN TO THE LABEL:ENDING; FROM HERE
 }