.TH goto "" "" "C Keyword"
.PC "Unconditionally jump within a function"
.PP
A
.B goto
command jumps to the area of the program introduced by
a label.
A program can
.B goto
only within a function;
to jump across function boundaries, you must use the functions
.B setjmp()
and
.BR longjmp() .
.PP
In the context of C programming,
the most common use for
.B goto
is to exit from a control block or go to the top of a control block.
It is used most often to write \*(QLripcord\*(QR routines, i.e.,
routines that are executed when a major error occurs too deeply within
a function for the program to disentangle itself correctly.
Note that in most instances,
.B goto
is a bad solution to a problem that can be better solved
by structured programming.
.SH Example
The following example demonstrates how to use
.BR goto .
.DM
#include <stdio.h>
.DE
.DM
main()
{
	char line[80];
.DE
.DM
getline:
	printf("Enter line: ");
	fflush(stdout);
	gets(line);
.DE
.DM
/* a series of tests often is best done with goto's */
	if (*line == 'x') {
		printf("Bad line\en");
		goto getline;
	} else if (*line == 'y') {
		printf("Try again\en");
		goto getline;
	} else if (*line == 'q')
		goto goodbye;
	else
		goto getline;

.DE
.DM
goodbye:
	printf("Goodbye.\en");
	exit(0);
}
.DE
.SH "See Also"
.Xr "C keywords" c_keyword
.br
\*(AS, \(sc7.6.6.1
.SH Notes
\fIThe C Programming Language\fR describes
.B goto
as \*(QLinfinitely-abusable\*(QR:
.IR "caveat utilitor" .
