Thursday, July 24, 2014

Relation between ‘unsigned char’ and ‘unsigned int’ in c++

Here goes the code:

int main(int argc, const char * argv[])
{
    unsigned char x = 'A';
    unsigned int y = 66;
    std::cout << "x is " << x << "\n";
    std::cout << "x is " << (unsigned int) x << "\n";
    std::cout << "y is " <<  y << "\n";
    std::cout << "y is " << (unsigned char) y << "\n";
    return 0;
}

Look up the ascii code table, ASCII Code – The extended ASCII table

65 101 41 01000001 A &#65; Uppercase A
66 102 42 01000010 B &#66; Uppercase B

As you can see, unsigned char and unsigned int are really the same under the hood!

0 comments: