• Home
  • 2.18 goto Statement

2.18 goto Statement

View Categories

2.18 goto Statement

< 1 min read

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.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *