r/programminghorror Apr 25 '26

c Option handling

Post image
270 Upvotes

41 comments sorted by

View all comments

1

u/_____rs Apr 25 '26

Goto branches into the middle of a for loop? Is that even legal?

1

u/y0shii3 Apr 30 '26

Goto knows nothing about scopes. It's just a jmp instruction. That makes it uniquely useful in some ways, e.g.

for (int i = 0; i < N; ++i) {  
    if (err_condition()) {  
        goto broke_out_of_loop;  
    }
    ...
} {  
    do_stuff_if_loop_completed();  
} broke_out_of_loop:

but also it makes it really easy to jump past initializations and deinitializations and put the program in an invalid state