Problem with "goto" in c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • noob15
    New Member
    • Jun 2010
    • 13

    Problem with "goto" in c

    Code:
    #include<stdio.h>
    int main()
    {
        int i=7;
        goto x;
        if(1){
            static int i=5;
            x:   printf("%d",i);
        }
        return 0;
    }
    output == 5

    Code:
    #include<stdio.h>
    int main()
    {
        int i=7;
        goto x;
        if(1){
            int i=5;
            x:   printf("%d",i);
        }
        return 0;
    }
    output == 134513723 (garbage value i guess)


    Code:
    #include<stdio.h>
    int main()
    {
        int i=0;
        
        if(1){
            int i=5;
            x:   printf("%d",i);
        }
         
        goto x;
        return 0;
    }
    output== infinite loop of 5

    Why garbage value in case 2 while not in case 1 and case 3
    and also why the value inside 'if' is getting printed since control of execution never reaches there ??..help !!
    ( compiler== gcc 4.4.3 )
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Why it should not reach if condition? It has to reach there.

    Case 1 and 3 looks correct, however i get answer as zero for 2nd case.

    Regards
    Dheeraj Joshi

    Comment

    • noob15
      New Member
      • Jun 2010
      • 13

      #3
      Originally posted by dheerajjoshim
      Why it should not reach if condition? It has to reach there.

      Case 1 and 3 looks correct, however i get answer as zero for 2nd case.

      Regards
      Dheeraj Joshi
      I'm not sure how goto statement works during the compiling phase...but i think it should directly jump to line 8 thus skipping the part "int i=5". Also any idea why case 2 giving 0 as output in your compiler ?

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        You defined i as static.

        Regards
        Dheeraj Joshi

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          1. Don't use goto
          2. DON'T USE goto
          3. DON'T USE goto


          Now assuming that you aren't actually intending to use goto but are just enquiring about the nature of a programming element.

          In case 1, i is a static variable. It exists for the lifetime of the program, although it is only in scope inside the if statement block. Because it has program lifetime it is created and initialised before main is entered so when you jump to the print statement i already has the correct value.

          In listing 2 i is not static, it is automatic and has lifetime of the block that contains it. The goto skips the statement that initialises it and you get a garbage value. Since it is accessing a possibly non-existent automatic variable that has not been initialised this is undefined behaviour and so should be avoided.

          In listing 3 the if statement is executed before the goto (and since 1 is true the block is executed). This initialises the memory used by i. However Strictly i only has lifetime of its containing block so when the goto statement alters the execution path back to the print statement i does not strictly exist although the printf access the same bit of memory that has already been initialised so it appears to print the correct value but is in fact undefined behaviour. Additionally since the goto jumps back up the code an infinite loop happens because nothing ever moves the follow of execution past the goto on line 11 to the return on line 12.


          And to answer your final question the flow of execution does reach the printf statements, that is precisely what the goto statements do, alter the flow of execution in a non-structured way to the label indicated.

          Comment

          • noob15
            New Member
            • Jun 2010
            • 13

            #6
            Originally posted by Banfa
            1. Don't use goto
            2. DON'T USE goto
            3. DON'T USE goto


            Now assuming that you aren't actually intending to use goto but are just enquiring about the nature of a programming element.

            In case 1, i is a static variable. It exists for the lifetime of the program, although it is only in scope inside the if statement block. Because it has program lifetime it is created and initialised before main is entered so when you jump to the print statement i already has the correct value.

            In listing 2 i is not static, it is automatic and has lifetime of the block that contains it. The goto skips the statement that initialises it and you get a garbage value. Since it is accessing a possibly non-existent automatic variable that has not been initialised this is undefined behaviour and so should be avoided.

            In listing 3 the if statement is executed before the goto (and since 1 is true the block is executed). This initialises the memory used by i. However Strictly i only has lifetime of its containing block so when the goto statement alters the execution path back to the print statement i does not strictly exist although the printf access the same bit of memory that has already been initialised so it appears to print the correct value but is in fact undefined behaviour. Additionally since the goto jumps back up the code an infinite loop happens because nothing ever moves the follow of execution past the goto on line 11 to the return on line 12.


            And to answer your final question the flow of execution does reach the printf statements, that is precisely what the goto statements do, alter the flow of execution in a non-structured way to the label indicated.
            Just inquiring about the nature of a programming element...thnx for the reply,cleared many things....i've lil doubt in case 2. Since the goto skips the statement that initializes i then why don't it read the value of i already present i.e. i=7 ?

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              That is because those 2 variables may have the same name (i) but they are not the same variable and exist in different locations.

              if you declare a variable with the same name as a variable that is in scope but declared in a different scope then you just hide that variable.

              Comment

              • noob15
                New Member
                • Jun 2010
                • 13

                #8
                Thanks Banfa and dheerajjoshim !! :)

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  a. Don't mask variables.
                  b. DON'T MASK variables.
                  c. DON'T MASK variables.
                  A reader is more likely to notice a goto instruction than redefinition of a variable. Confusion is guaranteed, so I consider masking a variable to be worse than using a goto.

                  Comment

                  Working...