c++numeric-limits

C++ Why numeric limits doesn't work for uint8_t and int8_t?


I recently noticed numeric_limits::max() and numeric_limits::min() don't seem to work for uint8_t and int8_t. Is there a reason for this or could it be a bug? I tried on my own computer using gcc compiler:

#include <iostream>
#include <limits>

using namespace std;

int main()
{

    std::cout << "numeric_limits<uint8_t>::max() = " << numeric_limits<uint8_t>::max() << std::endl; 
    std::cout << "numeric_limits<int8_t>::max() = " << numeric_limits<int8_t>::max() << std::endl; 
    std::cout << "numeric_limits<int8_t>::min() = " << numeric_limits<int8_t>::min() << std::endl;

    std::cout << "numeric_limits<uint16_t>::max() = " << numeric_limits<uint16_t>::max() << std::endl; 
    std::cout << "numeric_limits<int16_t>::max() = " << numeric_limits<int16_t>::max() << std::endl; 
    std::cout << "numeric_limits<int16_t>::min() = " << numeric_limits<int16_t>::min() << std::endl; 

    std::cout << "numeric_limits<uint32_t>::max() = " << numeric_limits<uint32_t>::max() << std::endl; 
    std::cout << "numeric_limits<int32_t>::max() = " << numeric_limits<int32_t>::max() << std::endl; 
    std::cout << "numeric_limits<int32_t>::min() = " << numeric_limits<int32_t>::min() << std::endl; 

    std::cout << "numeric_limits<uint64_t>::max() = " << numeric_limits<uint64_t>::max() << std::endl; 
    std::cout << "numeric_limits<int64_t>::max() = " << numeric_limits<int64_t>::max() << std::endl; 
    std::cout << "numeric_limits<int64_t>::min() = " << numeric_limits<int64_t>::min() << std::endl; 

    return 0;
}

gives output:

numeric_limits<uint8_t>::max() = �
numeric_limits<int8_t>::max() = 
numeric_limits<int8_t>::min() = �
numeric_limits<uint16_t>::max() = 65535
numeric_limits<int16_t>::max() = 32767
numeric_limits<int16_t>::min() = -32768
numeric_limits<uint32_t>::max() = 4294967295
numeric_limits<int32_t>::max() = 2147483647
numeric_limits<int32_t>::min() = -2147483648
numeric_limits<uint64_t>::max() = 18446744073709551615
numeric_limits<int64_t>::max() = 9223372036854775807
numeric_limits<int64_t>::min() = -9223372036854775808

Solution

  • It does work. The output is interpreted as ASCII characters though. If you cast to int before you print, you will see the correct values:

    std::cout << "numeric_limits<uint8_t>::max() = " << static_cast<int>(numeric_limits<uint8_t>::max()) << std::endl; 
    std::cout << "numeric_limits<int8_t>::max() = " << static_cast<int>(numeric_limits<int8_t>::max()) << std::endl; 
    std::cout << "numeric_limits<int8_t>::min() = " << static_cast<int>(numeric_limits<int8_t>::min()) << std::endl;