r/C_Programming • • Oct 17 '26

Goto is the worst command in C.

Goto is an old and hacky command in programming.

Goto was created in the Palaeozoic era. It was used for switching to line n.

Goto raises questions: “My editor doesn’t show line numbers, how can I use Goto?”

This command uncomfortable to use. Imagine, you have a book. You’re reading it, reading… And when you’re on page 5, it says: “Go to page 7, 10, 157, 21457, 8564588, 4…” Why?

You can replace Goto with:

  1. break/continue.
  2. return.
  3. Functions, in the end!

So, Goto is the worst command in all programming languages, including C.

MY OPINION MAY NOT COINCIDE WITH YOURS!!!

0 Upvotes

40 comments sorted by

19

u/Classic_Department42 Oct 17 '26

Error handling is best done with goto, read some linux kernel source

-22

u/just_pascal_dev Oct 17 '26

I think in high-level C it’s taboo.

10

u/Mr_Engineering Oct 17 '26

It's not.

Goto allows for easily navigable code using labels.

23

u/clickyclicky456 Oct 17 '26

Beginners: use goto all the time because they don't know any better, get into all sorts of tangles with it
Intermediate users: learn from their noob mistakes, "goto is a terrible thing, never ever use it ever under any circumstances"
Experts: understand that in the right circumstances (which are rare) it can be the right tool for the job, use it in those cases and no others

Sounds like you're still in the intermediate stage of your journey.

2

u/just_pascal_dev Oct 17 '26

I think you are right.

10

u/shinmai_rookie Oct 17 '26

Goto doesn't raise that question because in C you don't use line numbers, you go to labels which you define in the code and are shown alongside it.

And at any rate this discourse feels like it belongs in the 80s, that goto is to be used scarcely if at all is now common sense.

-2

u/just_pascal_dev Oct 17 '26

Oops, my mistake.

7

u/Future_Pace_5290 Oct 17 '26

You can replace Goto with:

break/continue.

return.

Functions, in the end

Exception handling needs it

1

u/WittyStick Oct 17 '26

Exception handling wants setjmp and longjmp, which is goto on steroids.

34

u/dmc_2930 Oct 17 '26

Why do you think goto requires line numbers? That’s not a thing in C.

1

u/Professional-Crow904 Oct 19 '26

You do that in BASIC. Most languages since mid-70s used labels to jump around, and it kind of became norm. Until Dijkstra wrote a long winded personal opinion piece sprinkled with valid technical and practical reasons to why he believed it was harmful. We now have morons on Reddit continuing the opinionated aspect of it with while silently ignoring any and all forms of technical reasoning.

41

u/Jonatan83 Oct 17 '26

If you can't find a use for goto you haven't written code with good error handling and cleanup

My editor doesn’t show line numbers, how can I use Goto

Ah so you haven't actually used goto, gotcha.

12

u/aldunax Oct 17 '26

The entire Linux kernel development community politely disagrees with you.

Also, worth a read. There are some very good, very legitimate use cases for goto.

2

u/Classic_Department42 Oct 18 '26

Also there was the great reply by Knuth 'structured programming with go to' to Wirth paper. A bit sad the article doesnr seem to mention it.

Another thing: early returns are considered a goto by structured programmer purist. 

7

u/komata_kya Oct 17 '26

Reminds me of my professor who said the same thing. He also used 1 based arrays in c++ by simply ignoring the 0th element...

6

u/Total-Box-5169 Oct 17 '26
  1. In C goto is not a command, goto is a statement.
  2. C never needed line numbers, those were used in early versions of BASIC.
  3. You are missing switch in your list of goto replacements, that statement is necessary to implement stackless coroutines.

4

u/tstanisl Oct 17 '26

The "goto" is a tool. Like every other useful tool it has some sharp edges, maybe a bit larger than other tools of its kind. But those sharp edges are exactly what makes tools useful even though a bit dangerous.

Generally, goto works nice for error handling in C. It's more concise, more readable and I would say even more elegant than other methods. Probably, advent of defer will change it but we need to wait for C2Y to assess.

Aditionally, some state machines can be naturally expressed in goto.

4

u/AliceCode Oct 17 '26

Goto is there for parity with assembly, which has jumps. It's just fundamentally how computers work. It's only a footgun if you don't know how to use it properly. If you don't like goto, then use a language that doesn't have it.

4

u/sci_ssor_ss Oct 17 '26

Skill issue, clearly

I mean:

Goto raises questions: “My editor doesn’t show line numbers, how can I use Goto?”

Whaaaat ?

This command uncomfortable to use. Imagine, you have a book. You’re reading it, reading… And when you’re on page 5, it says: “Go to page 7, 10, 157, 21457, 8564588, 4…” Why?

Read Rayuela, from Cortazar. It may help.

4

u/tux2603 Oct 17 '26

Goto in c uses labels, not line numbers. As long as you know what you're doing and understand how code actually runs there's no problems with it

Are you studying computer science?

3

u/gizahnl Oct 17 '26

Untill defer is common enough goto will be my goto cleanup routine, especially when having to deal with errors.

-3

u/mivanchev Oct 18 '26

There are destructors already available...

2

u/gizahnl Oct 18 '26

There are no destructors in C

0

u/mivanchev Oct 18 '26
__attribute__((cleanup(dtor)))

Why wait for a defer when one is right now easily implemented?

4

u/gizahnl Oct 18 '26

That's a GNU extension, also it doesn't have other variables in scope since it's a function call.
With defer you can check a variable on execution, and conditionally cleanup as needed.

0

u/mivanchev Oct 18 '26

cleanup is supported by both gcc and clan but I am really eager to see a defer referring to N variables from the scope.

3

u/rainboww_J Oct 17 '26

It really helps if a function needs a bit of a cleanup before a return. Without a goto you might end up doing a lot of things at multiple places (i.e. when you've done some setup stuff like opening files or what not and want to gracefully close them all when something went wrong. You can close those handles at every error in-between but a simple goto cleanup is much cleaner in my opinion).

As long as one isn't using goto in strange ways or let it make jumps all over the place it's fine

edit: also... why are you needing line numbers for goto? C isnt BASIC 😅

2

u/limitless_grow Oct 21 '26

You don't have to use goto but goto is great to jump to the end of a function and do the local cleanup once the result is known e.g. in case of errors. Any "great" workaround for preventing a goto makes your code worse to read. Just as in real life - once you are done in a shop you are just going to the exit where you pay.
Why do you want to do this in your programs a lot more idiotic than in real life?

Beside of that the 'switch' statement in C and C++ is nothing but just a multi-goto so don't ever use that! 😄

The following code of a test function with argument checking and going to the function exit in case of invalid arguments is valid for C and C++ compilers and uses 'goto' as told before but also that evil 'switch' statement.
Which of both makes that code hard to understand?

#include <stdio.h>

void test (int x)
{
   printf("%d:", x);

   if (x <= 0)
   {  /* we don't support negative numbers */
       printf(" invalid number");
       goto Exit; 
   }

   switch(x)
   {
             while(x--)
             {
    default:    printf(" %d", x);
                 
                if(x > 2)
                {
    case 1:        printf("*");
                }
             }
   }

   Exit:;
   printf("\n");
}


int main()
{
   int x = 5;

   while(x--)
     test(x);

   return (0);
}

2

u/dmc_2930 Oct 17 '26

I prefer the “comefrom” which is the opposite of goto.

(Also it’s not a real thing)

1

u/WittyStick Oct 17 '26 edited Oct 17 '26

defer is like comefrom with automatic labels. You comefrom the end of the next defer, or the end of the scope if there's no more defers.

defer { foo(); }
defer { bar(); }
baz();
return;

Is really:

scope_start:

    comefrom defer_bar_end;
    foo();
defer_foo_end:

    comefrom scope_end;
    bar();
defer_bar_end:

    comefrom scope_start;
    baz();

scope_end:
    comefrom defer_foo_end;
    return;

Of course, I jest (partially). Defer is really goto with automatic labels.

goto start;
    
defer_foo_start:
    foo();
    goto scope_end;
    
defer_bar_start:
    bar();
    goto defer_foo_start;

start:
    baz();
    goto defer_bar_start;
    
scope_end:
    return;

HUGE difference.

Unfortunately people are quick to jump on the bandwagon. We'll be quoting "Defer considered harmful" in future.

2

u/e-batters Oct 18 '26

I feel that error handling is a valid use case for goto: if (FAILED(...)) goto handle_error_and_cleanup;

It may not be useful apart from that, but for error handling goto is just fine.

1

u/Still_Explorer Oct 22 '26

I also when I first learnt programming, remember everyone would say that goto is bad. From personal experience I have found that there would be two times where it makes sense to use it, because it makes code very simple and straight-forward.

Though the same code can be rewritten without goto for sure, but kinda makes you consider the pros and cons. Such as adding extra code and more complicated logic (more execution paths) so there would be no goto statement at all.

When the break statement is ambiguous you do a goto. No problem to use a boolean variable instead outside `for` and break (last statement of the for loop) but you see the effort.

#include <stdio.h>

int main() {
    for (int i = 0; i < 100; i++) {
        switch (i) {
            case 10:
                goto breakfor;
            default:
                printf("%d\n", i);
                break;
        }
    }
    breakfor:
    return 0;
}

Also this one, you separate the algorithm from the execution paths. When you get an input you like/dislike you break the algorithm and stop the execution path. You wouldn't like to think about rewriting an algorithm from scratch to handle non-goto logic, though in a sense you can but why bother? You just goto and break from algorithm.

#include <stdio.h>

int main() {
    int count = 0;

    tryagain:
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            if (i == 1 && j == 1) {
                if (count < 10) {
                    count++;
                    goto tryagain;
                }
                else {
                    printf("done\n");
                    goto end;
                }
            }
        }
    }
    end:

    printf("%d\n", count);

    return 0;
}

1

u/bird_feeder_bird Oct 26 '26

I dont see the issue, its basically just jmp. Like if you want multiple paths to lead to the same place its very convenient.

1

u/non-existing-person Oct 17 '26

And when you’re on page 5, it says: “Go to page 7, 10, 157, 21457, 8564588, 4...

Not really. It's more like, you are on page 5, and it says "goto line 1,12,14 ON CURRENT page". goto does not go beyond single function, and functions should be fairly short anyway

-1

u/just_pascal_dev Oct 17 '26

Ok, guys. I made a mistake, when I said, that Goto is useless. My bad.

0

u/zsaleeba Oct 17 '26

OP, have you read the incredibly famous paper by Dijkstra, "Go To Statement Considered Harmful"?

0

u/WittyStick Oct 17 '26

Serious response though - I basically never use goto - there's virtually no need for it.

goto * on the other hand, is useful, but non-standard.

But now most uses of goto * can be replaced with musttail.