following code we always use and is alright,
while(int c=getchar() != 'q');
but if change the ints to pointers like below, compile will rise an error.
#include <stdio.h>
int main(){
int* c=0;
if(int* a =c !=0)
printf("ok");
}
error: cannot convert `bool' to `int*' in assignment
what had happened here? seems the priority was changed. can anyone give me a hint. and if I change it to this, it will work.
#include <stdio.h>
int main(){
int* c=0;
int* a;
if((a =c) !=0)
printf("ok");
}
No, operator precedence hasn’t changed between the two code snippets. The following:
is equivalent to:
Here,
getchar()is called, its return value is compared to'q', and the result of the comparison is stored in theintvariablec.In other words,
cdoesn’t contain the character that’s just been read; it contains a boolean indicator saying whether the most recent character was'q'.Since
cis never looked at, the code works. However, it is probably not what was intended.