一个关于C语言goto语句的问题
小弟在用C语言goto语句时出现了些问题,望各位大侠指教!//文件1voidmain(){...gotothere;...}//文件2voidtest(){...ther...
小弟在用C语言goto语句时出现了些问题,望各位大侠指教!
// 文件1
void main()
{
...
goto there;
...
}
// 文件2
void test()
{
...
there :
...
}
如上面两段程序所示,两个文件中使用goto语句跳转,编译器提示“未引用there标签”,肯定各位大侠指点迷津! 展开
// 文件1
void main()
{
...
goto there;
...
}
// 文件2
void test()
{
...
there :
...
}
如上面两段程序所示,两个文件中使用goto语句跳转,编译器提示“未引用there标签”,肯定各位大侠指点迷津! 展开
展开全部
goto语句也称为无条件转移语句,其一般格式如下: goto 语句标号;
其中语句标号是按标识符规定书写的符号, 放在某一语句行的前面,标号后加冒号(:)。语句标号起标识语句的作用,与goto 语句配合使用。
#include"stdio.h"
int main(void)
{
int n=0;
printf("input a string :\n");
loop: if(getchar()!='\n')
{
n++;
goto loop;
}
printf("%d",n);
}
goto语句在上例中的作用就是当接收用户输入的字符不是回车符的时候就跳转到if语句进行判断用户输入的字符,也就是说如果用户输入回车,那么就输入结束,转而执行printf("%d",n);
其中语句标号是按标识符规定书写的符号, 放在某一语句行的前面,标号后加冒号(:)。语句标号起标识语句的作用,与goto 语句配合使用。
#include"stdio.h"
int main(void)
{
int n=0;
printf("input a string :\n");
loop: if(getchar()!='\n')
{
n++;
goto loop;
}
printf("%d",n);
}
goto语句在上例中的作用就是当接收用户输入的字符不是回车符的时候就跳转到if语句进行判断用户输入的字符,也就是说如果用户输入回车,那么就输入结束,转而执行printf("%d",n);
展开全部
goto使用的标签 应该在同一个函数内吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
goto语句只可以用在同一个函数之类
如果这样可以跳转的话 那test函数之前的如果定义一些变量
后面要引用那岂不是无解了。
如果这样可以跳转的话 那test函数之前的如果定义一些变量
后面要引用那岂不是无解了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
goto跳转的语句只允许在函数内部跳转,不允许跳出函数,具体见《C99国际标准》中关于goto的定义:
6.8.6.1 The goto statement
Constraints
1 The identifier in a goto statement shall name a label located somewhere in the enclosing
function. A goto statement shall not jump from outside the scope of an identifier having
a variably modified type to inside the scope of that identifier.
Semantics
2 Agoto statement causes an unconditional jump to the statement prefixed by the named
label in the enclosing function.
3 EXAMPLE 1 It is sometimes convenient to jump into the middle of a complicated set of statements. The
following outline presents one possible approach to a problem based on these three assumptions:
1. The general initialization code accesses objects only visible to the current function.
2. The general initialization code is too large to warrant duplication.
3. The code to determine the next operation is at the head of the loop. (To allow it to be reached by
continue statements, for example.)
/* ... */
goto first_time;
for (;;) {
// determine next operation
/* ... */
if (need to reinitialize) {
// reinitialize-only code
/* ... */
first_time:
// general initialization code
/* ... */
continue;
}
// handle other operations
/* ... */
}
§6.8.6.1 Language 137
ISO/IEC 9899:1999 (E) .ISO/IEC
4 EXAMPLE 2 A goto statement is not allowed to jump past any declarations of objects with variably
modified types. A jump within the scope, however, is permitted.
goto lab3; // invalid: going INTO scope of VLA.
{
double a[n];
a[j] = 4.4;
lab3:
a[j] = 3.3;
goto lab4; // valid: going WITHIN scope of VLA.
a[j] = 5.5;
lab4:
a[j] = 6.6;
}
goto lab4; // invalid: going INTO scope of VLA.
6.8.6.1 The goto statement
Constraints
1 The identifier in a goto statement shall name a label located somewhere in the enclosing
function. A goto statement shall not jump from outside the scope of an identifier having
a variably modified type to inside the scope of that identifier.
Semantics
2 Agoto statement causes an unconditional jump to the statement prefixed by the named
label in the enclosing function.
3 EXAMPLE 1 It is sometimes convenient to jump into the middle of a complicated set of statements. The
following outline presents one possible approach to a problem based on these three assumptions:
1. The general initialization code accesses objects only visible to the current function.
2. The general initialization code is too large to warrant duplication.
3. The code to determine the next operation is at the head of the loop. (To allow it to be reached by
continue statements, for example.)
/* ... */
goto first_time;
for (;;) {
// determine next operation
/* ... */
if (need to reinitialize) {
// reinitialize-only code
/* ... */
first_time:
// general initialization code
/* ... */
continue;
}
// handle other operations
/* ... */
}
§6.8.6.1 Language 137
ISO/IEC 9899:1999 (E) .ISO/IEC
4 EXAMPLE 2 A goto statement is not allowed to jump past any declarations of objects with variably
modified types. A jump within the scope, however, is permitted.
goto lab3; // invalid: going INTO scope of VLA.
{
double a[n];
a[j] = 4.4;
lab3:
a[j] = 3.3;
goto lab4; // valid: going WITHIN scope of VLA.
a[j] = 5.5;
lab4:
a[j] = 6.6;
}
goto lab4; // invalid: going INTO scope of VLA.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询