logical expression question
code:
i = 7; j = 8; k = 9;
printf("%d", (i = j) || (j = k));
printf("\n");
printf("%d %d %d", i, j, k);
... i get
1
7 8 9
...why is the answer:
1
8 8 9
?
also...
code: i = 1; j = 1; k = 1;
printf("%d", ++1 || ++j && ++k);
printf("\n");
printf("%d %d %d", i, j, k);
...i get
1
1 1 1
.... why is the answer:
1
2 1 1
??
basically i'm wondering WHY 'i' is the only one being recalibrated (and what the reason for that is)
Re: logical expression question
Quote:
Originally posted by JohnMayer
[B]
Code:
i = 7; j = 8; k = 9;
printf("%d", (i = j) || (j = k));
printf("\n");
printf("%d %d %d", i, j, k);
Simple, you have
printf( "%d", (i = j) || (j = k));
Clearly you wanted to use Logical Comparisons. For that you need DOUBLE equals, to check equality... What you are doing in your printf is actually setting those values, not checking them. So try this
printf( "%d", (i == j) || (j == k));
Not sure what you even want to do here? Did you mean i for the first argument or 1? Also using some paranthesis would clear things up.
printf("%d", ++1 || ++j && ++k);