-1

So, I was practicing functions loops etc. and I stuck on doing this.I am pretty beginner on programming but I search a lot which makes me learn real faster. (By the way I tried my code to check if the input from user is number or not. )

printf("Define numbers.\n");
        printf("Select: ");
        scanf("%d", &x);
        printf("Select: ");
        scanf("%d", &y);
        if (y = temp)
        {
            printf("Division of something by 0 is undefined.\n");
        }
        else
        {
            printf("Division of %d and %d is %d\n", x, y, div(x, y));
        }

So I declared temp = 0 I thought it would do the trick but it didnt.

Ischys
  • 1
  • 2
  • 6
    Are you sure you didn't mean `if (y == temp)`? (`==` instead of `=`) – Fred Larson Oct 11 '17 at 21:36
  • `=` is assignment, rather than `==` which is comparison. You say `temp = 0` so what you're really doing is `if (y=temp)` --> `if(y=0)` --> `if (0=0)` --> `if(0)` which is false, so the `else` branch executes. And while magic numbers are generally frowned up, this case is an exception. Nothing gained in doing `int temp = 0;` and then `if (y == temp)` if that's your only use of temp. Might as well just do `if (y == 0)`. – yano Oct 11 '17 at 21:41

1 Answers1

1

when you do y = temp you are giving y the value of temp, in this case 0. In c that translates into a false logical value

What you want to do is

if (y == temp)

the == operator tests the equality between the 2 variables

Joao P
  • 38
  • 1
  • 6
  • 2
    0 translates to `false`, not `true`, which is why it isn't entering the if statement when temp is 0. – Christian Gibbons Oct 11 '17 at 21:40
  • `temp` is unnecessary here, amd may have been part of the OPs attempt to fix the problem. Worth noting perhaps that if you habitually use a literal zero and place it on the left-hand side, the compiler can catch the error: i.e. `if( 0 == y )`, because `if( y = 0 )` will compile but is seldom correct, while `if( 0 = y )` will fail to compile. – Clifford Oct 11 '17 at 22:36
  • This did the trick thank you I can't believe I missed that point. – Ischys Oct 12 '17 at 07:28