1.goto函数的作用域就是一个函数的作用域大小。注意,是函数的作用域,而不是花括号{ }。如下图,注释部分就是说明。如果在一个函数里加花括号{ }去验证,编译时会重复定义的错

C语言 goto 的作用域,用法
2、跨函数使用,如下所示,也会报错。一个是没有定义,一个是定义了未使用!!!
C语言 goto 的作用域,用法

3、这样一来,goto在使用时,在不同的函数,标签定义一样也是没有问题的。如下代码,输出结果。可见,goto 便签在不同函数定义一样是没有问题的。

#include <stdio.h>

void GoOne()
{
	printf("[%s,%d] ---- 111\n", __func__, __LINE__);
	goto test_one;
	
	printf("[%s,%d] ---- 222\n", __func__, __LINE__);
test_one:		
	printf("[%s,%d] ---- 333\n", __func__, __LINE__);
	return ;
}


void GoTwo()
{
	printf("[%s,%d] ---- 111\n", __func__, __LINE__);
	goto test_one;
	
	printf("[%s,%d] ---- 222\n", __func__, __LINE__);
test_one:		
	printf("[%s,%d] ---- 333\n", __func__, __LINE__);
	return ;
}
int main()
{
	GoOne();
	GoTwo();
	
	return 1;
}

C语言 goto 的作用域,用法

相关文章:

  • 2021-05-11
  • 2021-08-27
  • 2022-12-23
  • 2021-03-31
  • 2021-08-19
猜你喜欢
  • 2022-12-23
  • 2021-09-02
  • 2022-12-23
  • 2021-10-10
  • 2022-12-23
  • 2022-01-16
  • 2022-03-10
相关资源
相似解决方案