In chapter 3.9.1 "Safe conversions," from Stroustrup's Programming, he has code to illustrate a safe conversion from int to char and back.
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<cmath> using namespace std; int main() { char c='x'; int i1=c; int i2='x'; char c2 = i1; cout << c <<'<< i1 << ' << c2 << '\n'; return 0; } It's supposed to print x 120 xto the screen.
However, I can't get it to work and I'm not sure why. The result I get is x1764834364x.
I also get a 3 warnings (in Xcode 6.3.1).
- Multi-character character constant.
- Character constant too long for its type.
- Unused variable i2.
What causes this?
'<< i1 << 'is, as the message says, a multi-character character constant which is almost never what you want. Perhaps you wantcout << c << ' ' << i1 << ' ' << c2 << '\n';to add spaces between the things you're printing.