tags:

views:

92

answers:

4

Possible Duplicate:
Confusion about the output..

#include<stdio.h>
void main()
{
  int i=1,j=-1;
  if(printf("%d",i)<printf("%d",j))
    printf("%d",i);
  else
    printf("%d",j);
}

here in this program what is the output & how ?

A: 

printf returns the total number of characters written. "-1" is longer than "1". So…

Benoit
A: 

The program invokes undefined behaviour because main has to be defined at least as int main() and return either an int or exit has to be called. Especially the output might not be flushed and thus empty.

Supposing the full output appears, the exact output is undefined because the operands of the < can be evaluated in either order. The rest looks trivial.

Peter G.
In practice it will return an undefined value. Which has nothing to do with this question. I seriously doubt it would influence the output in any way. All in all, this is not an answer to the question, thus should be a comment.
Péter Török
@ Péter Török, have you noticed that no "\n" are output and the output typically will be line or block buffered?
Peter G.
A: 

answer is <<< 1 -1 1 >>>. Because printf statement returns int value ie number of character successfully written on screen{In if condition, first printf returns 1 and second printf return 2}. So that if condition becomes 1 < 2. This condition is true. So, execute the true block.

Muthuraman
A: 

The if statement needs to be evaluated to take the branch. For this both of the printf calls (made in if statement) will be executed in either order. This will cause 1 and -1 to be printed in o/p buffer but not guaranteed in which datum will be printed first. Now once value of if condition is known (false), the printf call inside else branch will be executed. It will print 1 in the buffer. At the end as part of exit handler, the o/p buffer will be flushed. This will cause 1-11 or -111 to be printed on the stdout.

Madhava Gaikwad