Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

goto — jump to a label

The goto statement unconditionally transfers control to a named label in the same function, including out of nested loops.

Version: C23; repository compiler flag, production runtime not attested

Syntax

goto label;
/* ... */
label:
    statement;

goto label; unconditionally jumps to label: in the same function. A label has function scope and may appear before or after the jump; this does not permit jumping between functions.

A jump must not enter the scope of an identifier with a variably modified type from outside that scope. This includes variable-length arrays (VLAs).

The example leaves both loops using goto found;. An ordinary break would leave only the inner loop. The expected output is found: 1, 2 followed by a newline.

Examples

#include <stdio.h>

int main(void)
{
    for (int row = 0; row < 3; ++row) {
        for (int col = 0; col < 3; ++col) {
            if (row == 1 && col == 2) {
                goto found;
            }
        }
    }
    puts("not found");
    return 0;

found:
    puts("found: 1, 2");
    return 0;
}

Result

found: 1, 2

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources