kenny@m.cs.uiuc.edu (01/11/90)
#include <stdio.h>
#include <stream.h>
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');
}
Question: Why does the first cout (cout << "1. x = " << *x << '\n';)
produce the output x = 1010 ??
The '\n', in single quotes, is promoted to an integer before being
passed to cout; the ASCII character value for newline is decimal 10.
This anomaly was an unavoidable misfeature of the original C++
calculus of types; it's fixed in cfront 2.0, gcc, and zortech.
A-T
jeffa@hpmwtd.HP.COM (Jeff Aguilera) (01/11/90)
> I am a new C++ user and curious as to why the following program behaves > as it does. > > The program: > ------------------------------------------------------------------------- > #include <stdio.h> > #include <stream.h> > > 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