#include<stdio.h>
int main(void)
{
int i=1,j=-1;
if((printf("%d",i))<(printf("%d",j)))
printf("%d",i);
else
printf("%d",j);
return 0;
}
As printf()
returns the number of characters successfully printed, the condition will be if(1<1)
which is false but the if part is executed and the output is 1 -1 1
. Why this is happening?
I think it is rather obvious: '1' is one character, '-1' is two. One is less than two.
printf returns the number of characters (not just digits) written.
So printf("%d",-1)
will return 2
not 1
Similarly printf("%d",1)
will return 1
Making the condition in the if
true.
Because printing j prints "-1", that's two characters. so 1<2 is true.
对于-1
,如果满足条件,则打印的字符数为2
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.