0

#include<stdio.h>  int main(){  for(printf("1");!printf("0");printf("2"))  printf("Sachin");  return 0; 

this is a code in C language the output is 10 I didn't get how the output is 10 can anyone explain please

25th Dec 2016, 7:42 AM
Arpitha G R
Arpitha G R - avatar
2 Respostas
+ 2
Whoa, I don't think I've seen THAT before. Regardless, I'll give you my best guess as to what's happening. First, printf doesn't return void - it returns an int. That's the reason it even works inside the header of a for loop in the first place. In the process of getting the return values for printf each time, it prints "1" and then "0". Since "printf(2)" is in the same place that "i++" would be in when in a normal for loop, the loop is already done and over with, since there's nothing accumulating. So the entire loop is skipped altogether, and thus, the output is "10". I'm not 100% sure what's happening here myself. If anyone else has a better explanation, I'd like to hear it.
25th Dec 2016, 8:30 AM
DaemonThread
DaemonThread - avatar
+ 1
The program will print 1, at the loop initialization step, then print 0 at the condition step, and as the condition !printf(0) is false because printf(0) returns a number different than 0, it will exit the loop, before the incrementing step (writing 2), and Sachin won't be written because the loop won't be entered. PS: the for loop could be written like that : for(;false;) { printf("Sachin"); printf(2); // increment step In this code, it's the same as : printf(1); printf(0); while(false) { printf("Sachin"); printf(2) }
25th Dec 2016, 8:35 AM
Thomas Ghobril