possible assignment in condition (C)
I have to find is the number "a" a two-digit odd. Mistake comes on if
#include <stdio.h>
main ()
{
int a,k;
int count=0;
printf ("input number \n", a);
scanf ("%d", &a);
k = a % 2;
while (a)
{
a /= 10;
count ++;
}
if (k = 1 &&a开发者_如何学Cmp; count = 2)
printf ("It is \n");
else
printf ("It is not \n");
return (0);
}
The error is here:
if (k = 1 && count = 2)
you probably meant:
if (k == 1 && count == 2)
=
is an assignment. ==
is a comparison for equality.
Also, the loop is not necessary. You can check if the number is two digits by checking if it's less than 100 and greater than or equal to 10.
GCC is complaining about this:
if (k = 1 && count = 2)
The equality operator is a double equals sign: ==
. What you've used, the single equals sign =
, is the assignment operator.
You are setting k
to 1 and count
to 2, and that if
will always be executed.
The message you're getting is designed to help people quickly catch exactly this problem.
精彩评论