goto statement leading to an infinite loop

Viewed 497

We were having a quiz of sorts and had the following question where we had to find the error, or if there's none, the output of the given code:

#include <stdio.h>

int main(void) {
  int n = 012;

  b: printf("%d\n",n--);

  if(n!=0){
    n--;
    goto b;
  }
  
  return 0;
}

I don't see anything wrong with this in theory but this leads to an infinite loop with the variable going way below 0. Could anyone help me with this?

6 Answers

You should only have one n-- statement. otherwise even if it reaches 0, when it get the first n-- it will be decremented one more time and will become -1. (so it will never match 0).

Hence, please change your code to

#include <stdio.h>

int main(void) {
  int n = 012;

  b: printf("%d\n",n--);

  if(n!=0){
   goto b;
  }
  
  return 0;
}
#include <stdio.h>

int main(void) {
  int n = 012;

  b: printf("%d\n",n);

  if(n!=0){
    n--;
    goto b;
  }
  
  return 0;
}

This would work. The version you posted does not work because it will never get to 0. It is subtracting once in the if statement, and once in the printf.

The condition if (n != 0) is always getting satisfied.

On each iteration the number n decreases by 2.

So when n = 2, it gets decremented once after printf("%d\n",n--)

then the condition if (n != 0) is true, as n = 1

Inside the condition block n is decremented once again, n = 0

So the next time before reaching the condition if (n != 0), n equals to -1,

which leads to an infinite loop.

In C 012 is an octal base number, in decimal it is equal to 10.

This code:

#include <stdio.h>

int main(void) {
  int n = 012;

  b: printf("%d\n",n--);

  if(n!=0){
    n--;
    goto b;
  }
  
  return 0;
}

Does this:

print n (10)
subtract 1 of n (n = 10 - 1 = 9)
check if n != 0 (9 != 0 -> true)
subtract 1 of n (n = 9 - 1 = 8)
jump to b

print n (8)
subtract 1 of n (n = 8 - 1 = 7)
check if n != 0 (7 != 0 -> true)
subtract 1 of n (n = 7 - 1 = 6)
jump to b

print n (6)
subtract 1 of n (n = 6 - 1 = 5)
check if n != 0 (5 != 0 -> true)
subtract 1 of n (n = 5 - 1 = 4)
jump to b

print n (4)
subtract 1 of n (n = 4 - 1 = 3)
check if n != 0 (3 != 0 -> true)
subtract 1 of n (n = 3 - 1 = 2)
jump to b

print n (2)
subtract 1 of n (n = 2 - 1 = 1)
check if n != 0 (1 != 0 -> true)
subtract 1 of n (n = 1 - 1 = 0)
jump to b

print n (0)
subtract 1 of n (n = 0 - 1 = -1)
check if n != 0 (-1 != 0 -> true)
subtract 1 of n (n = -1 - 1 = -2)
jump to b

print n (-2)
...
...
... goes on infinitely

Note that when n will never equal to 0 when the program checks if n != 0, because in this evaluation n is always an odd number since two subtractions by 1 are made every time.

Change from b: printf("%d\n",n--); to b: printf("%d\n",n);.

or

Change from

  if(n!=0){
    n--;
    goto b;

to

  if(n!=0){
    n;
    goto b;

Because n-- equals n = n - 1.

Your program:

#include <stdio.h>

int main(void) {
  int n = 012;

  b: printf("%d\n",n--); // n = n - 1

  if(n!=0){
    n--; // n = n - 1
    goto b;
  }
  
  return 0;
}

for out put [10, 8, 6, 4,2, 0]

    #include <stdio.h>

    int main(void) {
      int n = 012;

      b: printf("%d\n",n--);

      if(n>0){
        n--;
        goto b;
      }

      return 0;
    }

for [10,9,8,7,6,5,4,3,2,1]

    #include <stdio.h>

    int main(void) {
      int n = 012;

      b: printf("%d\n",n--);

      if(n>0){
        //n--;
        goto b;
      }

      return 0;
    }
Related