explain about why output 10 will come?
main()
{
for(printf("1");!printf("0");printf("2"))
{
printf("hello");
}
}
output:
10
According to its printf(3) man page, the printf
function returns
the number of characters printed (excluding the null byte used to end output to strings)
so, the initialization part of the for
is run once: printf("1")
; it puts the 1
character into the buffer of stdout
. Then the condition is evaluated !printf("0");
since printf("0")
is evaluated, it puts the 0
character into the buffer of stdout
and returns the number of output characters ie 1
so the condition is false, and the for
loop exists.
At last, main
is exiting the program, at that time only the stdout
buffers are flushed.
To be pedantic, your program would have a different behavior when, for unlikely reasons, the stdout
cannot be written successfully.
Because at printf("1") it executes the statement as it is. then printf("0") prints 0 on the screen and returns a value 1. when you negate it it gives a value equal to false in C. so the for loop meet its ending condition and exits.
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.