How to define labels for goto in C++ outside main function?

Is it possible to use the goto statement in C/C++ to jump to a label outside of the main function? Something like:

lab0: //First label
std::cout<<"Hello, ";
goto lab1;

lab1: //Second label
std::cout<<"World!";
goto lab2;

int main()
{
    goto lab0;
    lab2: //Third label
    std::cout<<std::endl;
    return 0;
}

Is there something I’m not getting about the goto and label statements?

  1. No, I don’t believe that is legal.
  2. You shouldn’t be using goto or labels. There are an exceedingly small number of cases where this these are the correct tools when working in c++.
  3. This forum is for C++ issue with respect to the Unreal Engine, it’s not meant as a general C++ support channel.
1 Like