Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!hplabs!hpl-opus!hpnmdla!hpmwtd!jeffa From: jeffa@hpmwtd.HP.COM (Jeff Aguilera) Newsgroups: comp.lang.c++ Subject: Re: Question Re. cout (Simple) Message-ID: <1520014@hpmwjaa.HP.COM> Date: 10 Jan 90 22:44:50 GMT References: <7567@hubcap.clemson.edu> Organization: HP Microwave Tech. - Santa Rosa, Ca. Lines: 44 > I am a new C++ user and curious as to why the following program behaves > as it does. > > The program: > ------------------------------------------------------------------------- > #include > #include > > main() > > { > int* x = new int; > > *x = 10; > cout << "1. x = " << *x << '\n'; > cout << "\n"; > cout << "2. x = " << *x << "\n"; > printf("3. x = %d %c",*x,'\n'); > } > ------------------------------------------------------------------------ > > > 1. x = 1010 > 2. x = 10 > 3. x = 10 > ------------------------------------------------------------------------ > > Question: Why does the first cout (cout << "1. x = " << *x << '\n';) > produce the output x = 1010 ?? > > Bala Because you are using C++ 1.2, which defines character constants as type int. That is, cout << '\n' calls ostream& operator<<(ostream&, int) not ostream& operator<<(ostream&, char*) With 2.0, character constants have type char, resulting in the expected behavior. ----- jeffa