doubt

jainprateek2000's Avatar, Join Date: Aug 2009
Newbie Member
Code: c++
#include<iostream.h>
void main()
{
float num=110;
(num!=(num=--num))?cout<<"OK":cout<<"NOT OK";
}
Give the output with detailed explanation

Last edited by SaswatPadhi; 25Aug2009 at 13:20.. Reason: Code-blocks
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Run it and see what is the ouput and if you cannot justify it ask us why the output is OK/NOT OK
0
c_user's Avatar, Join Date: Aug 2009
Contributor
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..
0
mayjune's Avatar, Join Date: Jun 2009
Invasive contributor
I agree with shabbir...
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
mayjune's Avatar, Join Date: Jun 2009
Invasive contributor
I think xp is right about this.. even i read something like this long back...