goto

How to Use the goto Jump Statement in C

In C, goto jumps to a label in the same function. Here is the C syntax, the rules that differ from C++, and the cleanup-on-error pattern C code uses.

On this page
  1. Syntax in C
  2. Where C is looser than C++
  3. Cleanup on error
  4. When a goto makes sense in C

Syntax in C

In C, goto is written the same way as in C++. Use the keyword, then a label name, and put the label in front of a statement in the same function:

#include <stdio.h>

int main(void) {
    printf("Start\n");
    goto end;
    printf("This line is skipped\n");

end:
    printf("End\n");
    return 0;
}

The output is:

Start
End

A label has to be followed by a statement. At the end of a block, write done: ; with the semicolon, because a closing brace is not a statement.

Where C is looser than C++

C allows a goto to jump forward past a variable declaration, even one with an initializer. The jump is legal, but the skipped variable never receives its value, so reading it before assigning it gives an unpredictable result:

#include <stdio.h>

int main(void) {
    goto skip;
    int count = 10;

skip:
    count = 20;
    printf("%d\n", count);
    return 0;
}

This program compiles as C and prints 20, because it assigns count before reading it. A C++ compiler rejects the same jump, since C++ does not allow skipping an initialized declaration. The rules for that are in how to use goto in c.

C has one firm restriction. A goto may not jump into the scope of a variable-length array, which is a local array whose size is set while the program runs. The compiler rejects that jump. Variable-length arrays are uncommon, so this rarely comes up.

Cleanup on error

In C, goto is most often used to release resources when something fails partway through a function. Each failure jumps to one cleanup section at the bottom, which releases whatever was acquired so far:

#include <stdio.h>
#include <stdlib.h>

int copy_first_line(const char *path) {
    int status = -1;
    FILE *file = NULL;
    char *buffer = NULL;

    buffer = malloc(256);
    if (buffer == NULL) {
        goto cleanup;
    }

    file = fopen(path, "r");
    if (file == NULL) {
        goto cleanup;
    }

    if (fgets(buffer, 256, file) == NULL) {
        goto cleanup;
    }

    printf("%s", buffer);
    status = 0;

cleanup:
    if (file != NULL) {
        fclose(file);
    }
    free(buffer);
    return status;
}

int main(void) {
    if (copy_first_line("notes.txt") != 0) {
        printf("Could not read the file\n");
        return 1;
    }
    return 0;
}

Four things make this pattern work:

  • Every variable is declared at the top, so no goto skips a declaration.
  • Pointers start as NULL, so the cleanup code can tell which resources exist. free accepts NULL, but fclose does not, so the file check is needed.
  • Each failure jumps to the same label, so the cleanup is written once.
  • status becomes 0 only after every step succeeds, so the return value shows the result.

To check that every path frees its memory, run the program under a memory checker. The steps for one are in how to use valgrind in c.

When a goto makes sense in C

Most loops and checks in C are clearer with break, continue, return and helper functions. A goto is easiest to defend when several failure points need the same cleanup, as in the example above.

The complete guide to goto

How to Use goto in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

How to Stringify a Macro in C++

Learn how the # operator turns a C++ macro argument into a string literal, with examples, the two-level trick for expanded macros and common pitfalls.

Macros 3 min read

Polymorphism in C++

Learn polymorphism in C++ with clear code examples: virtual functions, override, abstract classes, templates, and the mistakes beginners make most often.

Polymorphism 7 min read