Question:
In C, can "goto" jump to previous/upper labelled parts of a program or it can only jump to a subsequent part?
Sheer
2012-07-05 20:22:32 UTC
What I mean is, using "goto" statement we generally get out of a nested loop structure and pass control to a subsequent portion of program that matches the label.But can we do use it to pass control to a code segment that occurs BEFORE the goto statement and matches the label?As an analogy,just like "Continue" statement passes control to a section that comes BEFORE the "continue" statement, similarly, can "goto" pass control to a previous code segment (not essentially a loop as in "continue").....Your answer will be appreciated.
Four answers:
2012-07-05 20:34:27 UTC
Labels have function scope. You can jump to a label (using the goto statement) from anywhere providing it's defined within the same function as the goto statement.



> using "goto" statement we generally get out of a nested loop structure



If this happens, you should consider breaking your program up into functions. I try to avoid having more than three indentation levels in all my programs.



edit:



@Cronin, Silent: Would you say goto is unnecessary in the case shown below?



char *f(FILE *stream, size_t size)

{

        char *s;



        if (!(s = malloc(size))) {

                errflag = 0;

                return 0;

        }

        if (fread(s, size, 1, stream) != 1) {

                errflag = 1;

                goto error;

        }

        if (s[0] != 0x01) {

                errflag = 2;

                goto error;

        }

        return s;

error:

        free(s);

        return 0;

}



edit:



@Silent:

There are situations where it's necessary to use goto, and I'd say that this is certainly one of them. Suppose you increase the number of tests within the function and suppose that the common task performed at exit grows, would it really be beneficial to rewrite the common task for every single test in order to avoid an unconditional jump? An optimising compiler will remove the individual exit points anyway and you'll end up with code that's more or less like what I've written.



The benefits of using goto in this scenario are clear: It prevents you from making errors when you rewrite the common task over and over again, it's easier to modify the common task (since you only have to do it once), and your code will span less lines. I do not see why you would opt for an inferior option in this situation when an unconditional jump is clearly beneficial.
Silent
2012-07-06 03:27:52 UTC
In C, the goto keyword can be used to jump to any named label within the same function. The direction doesn't matter. The only restriction is that the label must be within the same function as the goto statement.



The goto keyword is never necessary; there is almost never a good reason to use it. Avoid it wherever possible.



Edit:

henni: yes, goto is unnecessary in that structure. Each goto statement can just be replaced with the two lines of code that appear after the "error" label.
?
2012-07-06 03:31:07 UTC
The rule of thumb is NEVER EVER use goto... so it's irrelevant how it works.



but yeah... you can goto a label before the goto statement.
?
2012-07-06 05:06:37 UTC
goto is not at all recommended to use inside nested loops. I doubt whether compiler accepts or not.



XYZ: ..

...

..

goto XYZ;



We can happily goto previous statements also.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...