Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Write a program in C++ that include limits header file, show the max and min limits of…

  1. User

    Write a program in C++ that include limits header file, show the max and min limits of all datatypes

  2. Assistant

    Sure! Here's a program that uses the <limits> header file to display the maximum and minimum limits of various data types in C++:

    #include <iostream>
    #include <limits>
    
    int main() {
        std::cout << "Minimum and Maximum Limits of C++ Data Types:" << std::endl;
        std::cout << "-------------------------------------------" << std::endl;
    
        std::cout << "Integer Types:" << std::endl;
        std::cout << "-----------------" << std::endl;
        std::cout << "Signed Integer Types:" << std::endl;
        std::cout << "char: " << std::numeric_limits<char>::min() << " to " << std::numeric_limits<char>::max() << std::endl;
        std::cout << "short: " << std::numeric_limits<short>::min() << " to " << std::numeric_limits<short>::max() << std::endl;
        std::cout << "int: " << std::numeric_limits<int>::min() << " to " << std::numeric_limits<int>::max() << std::endl;
        std::cout << "long: " << std::numeric_limits<long>::min() << " to " << std::numeric_limits<long>::max() << std::endl;
        std::cout << "long long: " << std::numeric_limits<long long>::min() << " to " << std::numeric_limits<long long>::max() << std::endl;
    
        std::cout << std::endl;
    
        std::cout << "Unsigned Integer Types:" << std::endl;
        std::cout << "unsigned char: " << static_cast<int>(std::numeric_limits<unsigned char>::min()) << " to " << static_cast<int>(std::numeric_limits<unsigned char>::max()) << std::endl;
        std::cout << "unsigned short: " << std::numeric_limits<unsigned short>::min() << " to " << std::numeric_limits<unsigned short>::max() << std::endl;
        std::cout << "unsigned int: " << std::numeric_limits<unsigned int>::min() << " to " << std::numeric_limits<unsigned int>::max() << std::endl;
        std::cout << "unsigned long: " << std::numeric_limits<unsigned long>::min() << " to " << std::numeric_limits<unsigned long>::max() << std::endl;
        std::cout << "unsigned long long: " << std::numeric_limits<unsigned long long>::min() << " to " << std::numeric_limits<unsigned long long>::max() << std::endl;
    
        std::cout << std::endl;
    
        std::cout << "Floating-Point Types:" << std::endl;
        std::cout << "---------------------" << std::endl;
        std::cout << "float: " << std::numeric_limits<float>::lowest() << " to " << std::numeric_limits<float>::max() << std::endl;
        std::cout << "double: " << std::numeric_limits<double>::lowest() << " to " << std::numeric_limits<double>::max() << std::endl;
        std::cout << "long double: " << std::numeric_limits<long double>::lowest() << " to " << std::numeric_limits<long double>::max() << std::endl;
    
        std::cout << std::endl;
    
        std::cout << "Boolean Type:" << std::endl;
        std::cout << "-------------" << std::endl;
        std::cout << "bool: " << std::boolalpha << std::numeric_limits<bool>::lowest() << " to " << std::numeric_limits<bool>::max() << std::endl;
    
        std::cout << std::endl;
    
        std::cout << "Character Type:" << std::endl;
        std::cout << "--------------" << std::endl;
        std::cout << "char16_t: " << std::numeric_limits<char16_t>::min() << " to " << std::numeric_limits<char16_t>::max() << std::endl;
        std::cout << "char32_t: " << std::numeric_limits<char32_t>::min() << " to " << std::numeric_limits<char32_t>::max() << std::endl;
        std::cout << "wchar_t: " << std::numeric_limits<wchar_t>::min() << " to " << std::numeric_limits<wchar_t>::max() << std::endl;
    
        return 0;
    }
    

    This program uses the std::numeric_limits function to retrieve the minimum and maximum limits of various C++ data types. The limits are then printed to the console using std::cout. The code also demonstrates handling of signed and unsigned integer types, floating-point types, boolean type, and wide character types (char16_t, char32_t, and wchar_t).

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.