Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   doubt (http://www.go4expert.com/forums/doubt-t19148/)

jainprateek2000 24Aug2009 22:06

doubt
 
Code: c++

#include<iostream.h>
void main()
{
float num=110;
(num!=(num=--num))?cout<<"OK":cout<<"NOT OK";
}

Give the output with detailed explanation

shabbir 24Aug2009 22:27

Re: doubt
 
Run it and see what is the ouput and if you cannot justify it ask us why the output is OK/NOT OK

c_user 25Aug2009 00:17

Re: doubt
 
as per my logic the answer is NOT OK...
because see the line
(num!=(num=--num))?cout<<"OK":cout<<"NOT OK";
is a ternary operator and
the words num=--num means that the num value is decreased by one and it is assigned to num
so now the new value of num is 109 and
the latter set of staments compares
109 with 109 [ since we know that -- operator decreases it by one so the answer is "not ok"

if u need more clear explanation i m ready to explain..
hav a gud day..

mayjune 25Aug2009 00:21

Re: doubt
 
I agree with shabbir...

xpi0t0s 25Aug2009 23:47

Re: doubt
 
The output is undefined because you modify the same variable twice in the same statement.
So the output will depend on how the compiler writer felt at the time, and may differ from compiler to compiler and version to version of the same compiler.

mayjune 25Aug2009 23:51

Re: doubt
 
I think xp is right about this.. even i read something like this long back...


All times are GMT +5.5. The time now is 23:46.