|
|
coherent
goto C Keyword goto
Unconditionally jump within a function
A goto command jumps to the area of the program introduced by a
label. A program can goto only within a function; to jump across
function boundaries, you must use the functions setjmp and
longjmp.
In the context of C programming, the most common use for goto is
to exit from a control block or go to the top of a control block.
It is used most often to write ``ripcord'' routines, i.e.,
routines that are executed when an major error occurs too deeply
within a function for the program to disentangle itself cor-
rectly. Note that in most instances, goto is a bad solution to a
problem that can be better solved by structured programming.
***** Example *****
The following example demonstrates how to use goto.
#include <stdio.h>
main()
{
char line[80];
getline:
printf("Enter line: ");
fflush(stdout);
gets(line);
/* a series of tests often is best done with goto's */
if (*line == 'x') {
printf("Bad line\n");
goto getline;
} else if (*line == 'y') {
printf("Try again\n");
goto getline;
} else if (*line == 'q')
goto goodbye;
else
goto getline;
COHERENT Lexicon Page 1
goto C Keyword goto
goodbye:
printf("Goodbye.\n");
exit(0);
}
***** See Also *****
C keywords
***** Notes *****
_T_h_e _C _P_r_o_g_r_a_m_m_i_n_g _L_a_n_g_u_a_g_e describes goto as ``infinitely-abus-
able'': caveat utilitor.
COHERENT Lexicon Page 2
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.