0

So I understand that C++ defines a byte as a typedef unsigned char. But I can't fully understand why, after setting a byte variable equal to a int, it couts as a smiley face..

#include <iostream>
#include <Windows.h>
using namespace std;

int main(){
    int x = 1;
    byte y = x;
    cout << y;
}

I'm sure this has a simple explanation, but I can't seem to find it. Is the first character of the unicode OR UTF8 set a smiley face?

  • And what stops you from looking up either Unicode or UTF8? – Kerrek SB Jun 09 '14 at 23:55
  • `byte` isn't even a valid type within the headers you're showing. Assuming it is what near-everyone defines it (`unsigned char`), you may do well to find how the `std::ostream` insertion operator for that type works. – WhozCraig Jun 09 '14 at 23:56
  • `char(1)` is SOH (start of heading) according to ascii table (which is not a printable character). – Jarod42 Jun 09 '14 at 23:56
  • Totally forgot about ASCII. I got it now, thanks so much. – user3724148 Jun 10 '14 at 00:02

1 Answers1

3

You must be using Windows. The character with a code of 1 is the smiley face in Code Page 437, the one used by command windows. Code points 0 through 31 do not correspond to printable characters in Unicode.

Yes, a byte is typically defined as an unsigned char. When you send a char variable (signed or unsigned) to cout, it outputs a character and not a number.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622