I can't understand why the following code outputs 10.
What I understand is that !printf("0") means !0, which is TRUE. So why doesn't the code print "Sachin"
#include <stdio.h>
int main() {
for (printf("1"); !printf("0"); printf("2"))
printf("Sachin");
return 0;
}
Output
10
let's analyze this side-effect loop statement:
1!printf("0")prints0, then sinceprintfreturns 1 because it just prints 1 character, the negation returns0and the loop is never entered because the condition is false right from the start. So neither2orSachinare printed.Of course, this code isn't practical, almost unreadable. So don't ever do things like this (
puts("10");is a good alternative for instance).more on the return value of
printf(that is often ignored):(from https://linux.die.net/man/3/printf)