-3

I created small code in C to test something. I thought that if i write 0 I will receive output ABC. But I cannot leave the loop. Please help me. Thank you.

#include<stdio.h>
#include<stdlib.h>

int main()
{

int x;

while(1)
    {
    printf("Enter number\n");
    scanf("%d",&x);
    if(x=0)
        goto stop;
    }

stop:
    printf("ABC");

return 0;
}
Peter
  • 449
  • 1
  • 3
  • 13

3 Answers3

3

Change the line

     if(x=0)

to

     if (x==0)

Originally you were assigning 0 to x, but you want to compare. To compare x with 0 you need to use ==

smushi
  • 701
  • 6
  • 17
2

the code is wrong the code

if(x = 0 )

should change to

if(x == 0 )

cijianzy
  • 71
  • 2
  • 8
0

As the others pointed out, a single equal sign in c is assignment, and a double equals sign tests for equality. In this case, the double equals sign is needed. So instead of if(x = 0), you want if(x == 0)
One more tip, using break is preferable to a goto statement here. Hope this helps.

Stoud
  • 283
  • 1
  • 7
  • 15