COHERENT manpages

This page displays the COHERENT manpage for goto [Unconditionally jump within a function].

List of available manpages
Index


goto -- C Keyword

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
a  major error  occurs too  deeply  within a  function for  the program  to
disentangle itself  correctly.  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;


goodbye:
    printf("Goodbye.\n");
    exit(0);
}

See Also

C keywords
ANSI Standard, §7.6.6.1

Notes

The  C  Programming  Language  describes goto  as  ``infinitely-abusable'':
caveat utilitor.