Table of Contents
The goto statement transfers execution directly to a labeled statement within the same function. The destination is specified using a label followed by a colon.
#include <stdio.h>
int main()
{
int number = 0;
if (number == 0)
{
goto zero;
}
printf("This statement is skipped.\n");
zero:
printf("Number is zero.\n");
return 0;
}
Example Output #
Number is zero.
Explanation #
The goto statement transfers control to the label named zero:
goto zero;
The destination is defined by:
zero:
Because execution jumps directly to this label, the following statement is skipped:
printf("This statement is skipped.\n");
A goto statement can only transfer control to a label within the same function. Labels themselves are covered separately in the next lesson.