-4

I have some question about goto.Here is my code:

int main(){ 
    int a = 1;
    if (a<0)
        goto out;
    out:
    printf("out");
    return 1;
}

The problem is, whatever the value a is (eg:a=-1 or a=0), the out could printed.
Could anyone tell me the reason, thank you very much.

Kayathiri
  • 779
  • 1
  • 15
  • 26
  • 4
    Because there is nothing in the code that would cause the `printf` line to be skipped. The `goto` does nothing as it just causes the next line to be executed - which would happen regardless of whether the `goto` is run or not. – kaylum May 30 '16 at 03:43
  • 1
    I don't understand the motivation for this question, unless the OP is confused about conditional execution, or signed comparison. Regardless of what value you set `a` to, you will end up at the `out:` label. If `out:` appeared *after* `printf("out")`, you might get different program behaviour depending on the value of `(a)`... – Brett Hale May 30 '16 at 05:09
  • There are two cases, right? (1) If `a<0` then the `goto` is executed and control is transferred to the `out` label, and `out` is printed. (2) if `a >= 0` then the goto is skipped, and control passes to the first statement that follows the `if` statement. But that's just the statement with the `out` label, and again `out` will be printed. There's no path through the function that avoids it. How is this confusing? – Tom Karzes May 30 '16 at 05:27
  • If I was of a suspicious nature, I would say that this question is voting-ring or puppet food. – Martin James May 30 '16 at 08:40
  • This is not a scenario for using a `goto`, because `printf()` is getting executed so and so, with or without your `goto`. (-: – user3078414 May 30 '16 at 08:57

6 Answers6

8

Assume that the value of a is 1, in which case, a<0 is false, so the if-statement goto out; is skipped and the program resumes sequential execution. The next statement is obviously:

out:
    printf("out");

Note that out: is just a label, so the printf() statement is executed. Thus, out is printed, just because it is the next sequential step in the execution of the program (and not because the if condition was true).

If the value of a is, say -1, in which case a<0 is true, so the if-statement, goto out; is executed and the control goes to:

out:
    printf("out");

Thus in both the cases, out is printed.

To understand better, consider the following example:

#include <cstdio>

int main(){ 
    int a = -1;

    if (a<0)
        goto in;

    out:
    printf("out");

    in:
    printf("in");

    return 0;
}

Here, since the value of a is -1, a<0 will be true and so, goto in; will be executed. This will take the control of execution to the following statement:

in:
        printf("in");

Thus, the output of the above code snippet is in.

Live demo of the above code is here.

P.S.: Using a goto statement breaks the normal flow of the program, and hence using it is considered as a bad practise.

Community
  • 1
  • 1
abhishek_naik
  • 1,287
  • 2
  • 15
  • 27
6

Regardless if the 'if' statement is entered or not, the next statement will be the printf statement.

If it skips the 'if statement', printf comes up next, if the 'if statement' is entered, then it goto's the printf. Either way the statement will display.

The 'out' is kind of like an index that the assembly keeps track of. If the goto 'out' weren't there, the printf would still be there, just without the memory location that the compiler keeps track of.

brw59
  • 502
  • 4
  • 19
4

If a < 0. you hit the goto, which takes you to the labeled code. If a >= 0, you skip the goto... but the next line is the labeled code, anyway. Labeled code doesn't -only- get executed when a goto brings the execution path there.

2

Firstly, you should avoid using goto when you can.

You probably want an if else or at least a bracketed if

 int main(){ 
    int a = 1;
    if (a<0){
        printf("out");
    }
}

OR

int main(){ 
    int a = 1;
    if (a<0){
        printf("out");
    }
    else {
         print("in");
    }
    return 1;
}
Ash
  • 5,786
  • 5
  • 22
  • 42
  • Thanks @BatCoder. I read the question as: Why is my `goto` statement not working as expected? While everyone else's answer will get him to use `goto` properly, I was focusing on his ultimate requirement and the best solution for it. – Ash May 30 '16 at 04:01
2

There are two types of jumps that goto can perform. 1 --> Forward jump 2 --> Backward jump

You could try the following program:

   #include<conio.h>
   #include<stdio.h>
   #include<stdio.h>
   main()
   { 
     int i=1;
     goto x;
     y: 
     printf("This is the 1st goto statement\n");
     goto z;
     x:
     if(i==1)
     {
      printf("This is the 2nd goto statement\n");
      goto y;   
     }
     z:
     printf("End of the program\n");
   } 

NOTE: While I agree that this is good enough in knowledge point of view, I strongly advise you not to use goto statements as it is a very bad programming practice. Also please go through the link Problems that can arise from using a GOTO statement to jump down only? which has some debate about the use of goto statement.

Community
  • 1
  • 1
Coding Ninja
  • 114
  • 12
1
int main(){ 
    int a = 1;                    // 1
    if (a<0)                      // 2
        goto out;                 // 3
    out:                          // 4
    printf("out");                // 5
    return 1;
}

When your code is executed, it is executed from top to bottom. When inserting things like conditions (if-else, switch case), loops (while, for) or other special statements (like your goto) the code can have an indirect flow, meaning, it can, sometimes, stray from the main route.

Now, if you look at your code without lines 2-4, you get a simple printf code. If you add those lines back, you get a new option of code execution - if the (a<0) is true, then the PC will move to where you point it, instead of going downward, as it would otherwise.

The reason your code always goes to the printf is simple: if (a<0) it true, the "goto" send the PC to the printf. Else, the standard flow of the code will send the PC there.

As mentioned in other answers, if you wnat to avoid printf when (a>0) is false you can use:

int main(){ 
int a = 1;
if (a<0)
  printf("out");
/* else
   printf("in"); */ 
return 1;
}

Last note: using goto is considered bad because... well because it says so here (it causes spaghetti code)

Community
  • 1
  • 1
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124