doubt

Newbie Member
24Aug2009,22:06   #1
jainprateek2000's Avatar
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
Go4Expert Founder
24Aug2009,22:27   #2
shabbir's Avatar
Run it and see what is the ouput and if you cannot justify it ask us why the output is OK/NOT OK
Contributor
25Aug2009,00:17   #3
c_user's Avatar
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..
Invasive contributor
25Aug2009,00:21   #4
mayjune's Avatar
I agree with shabbir...
Mentor
25Aug2009,23:47   #5
xpi0t0s's Avatar
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.
Invasive contributor
25Aug2009,23:51   #6
mayjune's Avatar
I think xp is right about this.. even i read something like this long back...