Why do fixed-width integers print characters instead of ints?

Given the following code.

#include <cstdint>
#include <iostream>
#include <limits>

int main()
{
    int8_t x = 5;
    std::cout << x << '\n';

    int y = 5;
    std::cout << y;

    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();

    return 0;
}

My conclusion is three-leafed and 5. If fixed-width integers are integers, why do they output their ASCII character?

Edit: just found out that this behavior only happens for 8-bit fixed-width integers? Is this compiler behavior?

+4
source share
2 answers

Well, they are integers in the sense that you can perform 8-bit integer arithmetic with them.

, -, int8_t typedef signed char. , signed char , , operator<< signed char , . , - signed char.

int8_t, int .

+12

() intN_t, , uint_fast16_t, uintmax_t intptr_t, . , , .

" 8 " , , 8 , char (, ). , int8_t, , char signed char.

, iostreams operator<< , , ( , std::cout << 'a'), int8_t - , . (, , signed char, , char , .) , .

, , . , : int8_t x = 5; std::cout << +x;

+2

All Articles