如何正确指定 goto 语句的标签?

How do I properly specify a label for the goto statement?

本文关键字:标签 语句 goto 何正确      更新时间:2023-10-16

我在程序中使用了goto语句。但是,当我尝试编译程序时,显示一条错误消息:expected primary-expression before ‘:’ token

#include <iostream>
using namespace std;
int main(void)
{
int num1, ans;
cout << "Please input a number: ";
cin >> num1;
if(num1 < 10)
goto tmp;
printf("Hello.n");
:tmp
printf("Worldn");
return 0;
}

冒号位于标签的另一侧,如下所示:

tmp:
printf("Worldn");