Char-to-int conversion in C++ -
in chapter 3.9.1 "safe conversions," stroustrup's programming, has code illustrate safe conversion int char , 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 print x 120 xto screen.
however, can't work , i'm not sure why. result x1764834364x.
i 3 warnings (in xcode 6.3.1).
- multi-character character constant.
- character constant long type.
- unused variable i2.
what causes this?
the problem here:
'<< i1 << ' the compiler gives warning (gcc @ least):
warning: character constant long type
it believes trying display single char (the content between apostrophes), in fact passing multiple chars. want add spaces, like
' ' << i1 << ' '
Comments
Post a Comment