Search code examples
c++chariostreamcoutunsigned

why am getting strange output "÷" when performing the simple follwoing c++ lines


When running this code:

#include <iostream>
using namespace std;

int main() {
    // Signed char
    signed char signedCharValue = -10;
    cout << "Signed char: " << signedCharValue << endl;

    // Unsigned char
    unsigned char unsignedCharValue = 246;
    cout << "Unsigned char: " << unsignedCharValue << endl;

    return 0;
}


i'm expecting:

Signed char: -10
Unsigned char: 246

i'm getting:

Signed char: ÷
Unsigned char: ÷

Solution

  • It's because this is C++, not C.

    char is a distinct type, and the << operator that is used for the output has an overload for char, to output it as such.

    If you want the codes, cast it to int.

    cout << "Signed char: " << (int)signedCharValue << endl;