Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: A question ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 12 of 25 Topic 1007 of 1134
Post > Topic >>

Re: A question about '=='' in C++!

by Jack Klein <jackklein@[EMAIL PROTECTED] > Jan 12, 2008 at 04:43 PM

On 21 Sep 2007 05:15:35 GMT, "Guofu Chen" <gchen@[EMAIL PROTECTED]
> wrote
in comp.lang.c.moderated:

> Can anyone tell me the different between

The C language does not define anything about C++ at all.  The place
to ask this question is comp.lang.c++ or comp.lang.c++ moderated.
Nevertheless, since it is here and moderation is slow, I will provide
some insight into what would happen if this were C, and no guarantees
that C++ is the same.

> (1)
> int i, j;
> i = j ==1;

This is undefined behavior, since you are testing to see if the value
of 'j' is equal to 1, but 'j' is uninitialized so using its value,
even to compare to a constant like 1 is undefined.

> cout << i;
> cout << j;

Here you are again accessing the value of 'j', which is still
uninitialized.  Due to the undefined behavior, there is no correct or
incorrect output.

> and
> (2)
> int i, j;
> i = j = 1;

The statement above sets the value of both 'i' and 'j' to 1.  What is
puzzling about it?

> cout << i;
> cout << j;
> 
> 
> Why the result of  (1) is
> 
> 0 0

Undefined behavior can produce any result.

> while the result of (2) is
> 
> 1 1

Because you set the value of both 'i' and 'j' to 1.  Technically
speaking, the statement:

   i = j = 1;

....sets 'j' to 1 and then sets 'i' to the value of 'j'.

> Thanks! 

It seems as though you have some confusion on the meaning of the
assignment operator ('=') and the comparison operator ('==').  Best
consult your text book or other language reference.

And post C++ questions to a C++ newsgroup in the future.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.para****ft.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
-- 
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
 -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
 




 25 Posts in Topic:
A question about '=='' in C++!
"Guofu Chen" &l  2007-09-21 05:15:35 
Re: A question about '=='' in C++!
Erik Max Francis <max@  2008-01-12 16:23:47 
Re: A question about '=='' in C++!
Keith Thompson <kst-u@  2008-01-12 16:41:25 
Re: A question about '=='' in C++!
Kenneth Brody <kenbrod  2008-01-12 16:41:51 
Re: A question about '=='' in C++!
Andrei Voropaev <avoro  2008-01-12 16:41:48 
Re: A question about '=='' in C++!
WillerZ <newsreplies@[  2008-01-12 16:41:44 
Re: A question about '=='' in C++!
REH <spamjunk@[EMAIL P  2008-02-17 12:06:12 
Re: A question about '=='' in C++!
"Douglas A. Gwyn&quo  2008-01-12 16:42:24 
Re: A question about '=='' in C++!
Daniel Weber <dw2007@[  2008-01-12 16:42:52 
Re: A question about '=='' in C++!
Barry Schwarz <schwarz  2008-01-12 16:43:19 
Re: A question about '=='' in C++!
kushal_kumaran <kushal  2008-01-12 16:44:17 
Re: A question about '=='' in C++!
Jack Klein <jackklein@  2008-01-12 16:43:35 
Re: A question about '=='' in C++!
Jensen Somers <usenet@  2008-01-12 16:43:45 
Re: A question about '=='' in C++!
VA <buvaira@[EMAIL PRO  2008-01-12 16:44:33 
Re: A question about '=='' in C++!
moschops <moschop@[EMA  2008-01-12 16:45:04 
Re: A question about '=='' in C++!
ta0kira@[EMAIL PROTECTED]  2008-01-12 16:45:39 
Re: A question about '=='' in C++!
Anabelle Collins <nano  2008-01-12 16:45:44 
Re: A question about '=='' in C++!
Joel Yliluoma <bisqwit  2008-01-12 16:46:10 
Re: A question about '=='' in C++!
Barry Schwarz <schwarz  2008-01-12 16:47:47 
Re: A question about '=='' in C++!
jk <jimktrains@[EMAIL   2008-01-12 18:24:26 
Re: A question about '=='' in C++!
hex <iostream64@[EMAIL  2008-01-12 19:01:16 
Re: A question about '=='' in C++!
klausboy2004@[EMAIL PROTE  2008-01-12 19:01:44 
Re: A question about '=='' in C++!
Dave Neary <dneary@[EM  2008-01-12 19:01:50 
Re: A question about '=='' in C++!
Matthias Buelow <mkb@[  2008-01-12 19:03:03 
Re: A question about '=='' in C++!
Sc0rpi0 <sc0rpi0@[EMAI  2008-01-12 19:03:06 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:20:42 CDT 2008.