0

In the following C code, f calls g, and g calls h. Notice the goto in h, however: it will jump back to f if a satisfies a certain condition.

void h(int a)
{
    if (a > 10)
        goto out;
}

void g(int a, int b)
{
    h(a);
}

void f(int a, int b)
{
    g(a, b);
    return;
out:
    printf("b: %d\n", b);
}

My question is: how will the stack be if the goto is triggered? Will g and h be unstacked? And will f still print the right value of b? (or will it print it right only in some cases when I am lucky?)

(Please, I don't want to discuss if this is a good practice, or if this should be used at all. Also, consider that the actual code is complicated enough so that the compiler won't be smart enough to, e.g., optimize g out)

[I can give details on why I am doing this, if it matters -- I don't think it does]

5
  • 3
    AFAIK labels must be locale to function scope. You can't do that in standard C.
    – LPs
    Jun 20, 2016 at 12:54
  • Your code doesn't compile for me, like LPs comment states, gotos are locale to function scopes Jun 20, 2016 at 12:54
  • 2
    Possible duplicate of How to "goto" into different function in c?
    – LPs
    Jun 20, 2016 at 12:55
  • 1
    There is no stack in the C standard, your question lacks a foundation. And your code is invalid. Jun 20, 2016 at 12:56
  • It is horribly bad to jump from code within one function to code in another. Jun 20, 2016 at 13:56

4 Answers 4

4

This will result in undefined behavior in standard C.

From 6.8.6.1/1 of the C Language Standard:

The identifier in a goto statement shall name a label located somewhere in the enclosing function. A goto statement shall not jump from outside the scope of an identifier having a variably modified type to inside the scope of that identifier.

4

The question is void, because it simply cannot be done like this: you only can goto within a function, not between functions.

For jumping between functions, you can use setjmp/longjmp.

2
  • There is also no need for a stack in C from the language standard. Jun 20, 2016 at 12:59
  • .. where the fun thing of setjmp/longjmp is that their designer was well aware of potential stack problems (in implementations that use it) so they were designed to not use the same stack as regular functions. (See lots and lots of Stack Overflow questions, such as this one.)
    – Jongware
    Jun 20, 2016 at 13:01
1

A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled statement in the same function.

Labels are local to a single function, you cannot jump between different functions.

NOTE − In my opinion the use of goto statement is highly discouraged.

ref: http://www.tutorialspoint.com/cprogramming/c_goto_statement.htm

9
  • 3
    "Use of goto statement is highly discouraged" is a subjective. I agree to it only for the "general" case. There are cases where using goto is the easiest way to go, e. g. error handling.
    – glglgl
    Jun 20, 2016 at 12:57
  • I agree @glglgl. The Linux kernel is full of examples where it is fit Jun 20, 2016 at 12:58
  • goto has its applications in C. So no, it is not discouraged. That's what CS profs tell the students. The profs with some insight in practical programming later weaken this statement. Jun 20, 2016 at 12:58
  • @IshayPeled Linux Kernel is far from a good example to "how to code".... my personal opinion.
    – LPs
    Jun 20, 2016 at 13:05
  • 1
    @glglgl Can agree, but is the easiest way for who?.. Probably for lazy coders... ;)
    – LPs
    Jun 20, 2016 at 13:06
-1

You cannot do this since labels are local to each particular function. However, the nearest standard equivalent is the setjmp() and longjmp() pair of functions. That should work. :)

2
  • 1
    setjmp() and longjmp() are not exactly equivalents IMHO..
    – sjsam
    Jun 20, 2016 at 12:59
  • I agree but for a beginner's needs, it should suffice. :)
    – Arjun Raju
    Jun 20, 2016 at 13:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.