The goto statement

A goto statement causes your program to unconditionally transfer control to the statement that is associated with the label specified on the goto statement.

goto statement syntax

Read syntax diagramSkip visual syntax diagramgotolabel_identifier;

Because the goto statement can interfere with the normal sequence of processing, it makes a program more difficult to read and maintain. Often, a break statement, a continue statement, or a function call can eliminate the need for a goto statement.

If an active block is exited using a goto statement, any local variables are destroyed when control is transferred from that block.

You cannot use a goto statement to jump over initializations.

C only A goto statement is allowed to jump within the scope of a variable length array, but not past any declarations of objects with variably modified types. C only

The following example shows a goto statement that is used to jump out of a nested loop. This function could be written without using a goto statement.

CCNRAA6

/**
 ** This example shows a goto statement that is used to
 ** jump out of a nested loop.
 **/

#include <stdio.h>
void display(int matrix[3][3]);

int main(void)
{
   int matrix[3][3]=    {1,2,3,4,5,2,8,9,10};
   display(matrix);
   return(0);
}

void display(int matrix[3][3])
{
   int i, j;

   for (i = 0; i < 3; i++)
      for (j = 0; j < 3; j++)
      {
         if ( (matrix[i][j] < 1) || (matrix[i][j] > 6) )
            goto out_of_bounds;
         printf("matrix[%d][%d] = %d\n", i, j, matrix[i][j]);
      }
   return;
   out_of_bounds: printf("number must be 1 through 6\n");
}

Computed goto statement (IBM extension)

A computed goto is a goto statement for which the target is a label from the same function. The address of the label is a constant of type void*, and is obtained by applying the unary label value operator && to the label. The target of a computed goto is known at run time, and all computed goto statements from the same function will have the same targets. The language feature is an extension to C99 and C++, implemented to facilitate porting programs developed with GNU C.

Computed goto statement syntax

Read syntax diagramSkip visual syntax diagramgoto*expression ;
The *expression is an expression of type void*.