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 ?
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 ?
printf
returns the total number of characters written. "-1" is longer than "1". So…
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.
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.
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.