-1

I'm studying C++ using The C++ Programming Language from Bjarne Stroustrup, and while doing exercise 4.5 (What, on your system, are the largest and the smallest values of the following types: char, short, int, long , float, double, long double, and unsigned) I've wrote:

#include <iostream>
#include <limits>

using namespace std;
int main(){
// (...)
// Exercise 4.11.4:
    
    cout << "\nExercise 4.11.4:\n";
    for (int i = 48; i < 127; i++)
    {
        cout
            << char(i) << " Hex:" << hex << i << " Dec:" << dec << i << " Oct:" << oct << i << "" << endl;
    }
    cout << "\nExercise 4.11.5:\n";

    cout << "Largest and smallest values for different data types:\n";

    // char ???!??!?!?!?!?
    cout << "char: " << static_cast<int>(numeric_limits<char>::min()) << " to "
         << static_cast<int>(numeric_limits<char>::max()) << endl;

    // unsigned char
    cout << "unsigned char: " << static_cast<int>(numeric_limits<unsigned char>::min()) << " to "
         << static_cast<int>(numeric_limits<unsigned char>::max()) << endl;

    // short
    cout << "short: " << numeric_limits<short>::min() << " to "
         << numeric_limits<short>::max() << endl;

    // int
    cout << "int: " << numeric_limits<int>::min() << " to "
         << numeric_limits<int>::max() << endl;

    // float
    cout << "float: " << numeric_limits<float>::lowest() << " to "
         << numeric_limits<float>::max() << endl;

    // double
    cout << "double: " << numeric_limits<double>::lowest() << " to "
         << numeric_limits<double>::max() << endl;

    // long double
    cout << "long double: " << numeric_limits<long double>::lowest() << " to "
         << numeric_limits<long double>::max() << endl;

    // unsigned
    cout << "unsigned: " << 0 << " to "
         << numeric_limits<unsigned>::max() << endl;
    }

And the output is REALLY weird:

(...)
} Hex:7d Dec:125 Oct:175
~ Hex:7e Dec:126 Oct:176

Exercise 4.11.5:
Largest and smallest values for different data types:
char: 37777777600 to 177
unsigned char: 0 to 377
short: 100000 to 77777
int: 20000000000 to 17777777777
float: -3.40282e+38 to 3.40282e+38
double: -1.79769e+308 to 1.79769e+308
long double: -1.79769e+308 to 1.79769e+308
unsigned: 0 to 37777777777

Does anybody has a clue why the values are wrong? I was expecting char to be -128 to 127.

EDIT NOTE: Added part of missing code that was causing the issue, so now it is possible to replicate the problem.

10
  • BTW, octal 177 = 127, so result is correct, but in octal Commented Dec 26, 2023 at 5:31
  • how come you are getting OCTAL output? Commented Dec 26, 2023 at 5:31
  • 1
    "last exercise in the same file" ?? So instead of your actual code, you showed us a made-up one that you didn't actually test? This is a big no-no. Commented Dec 26, 2023 at 5:44
  • 2
    It's a good thing that you removed the irrelevant parts, but you should re-test the code after that. Commented Dec 26, 2023 at 5:54
  • 1
    These links may help you : learncpp.com, en.cppreference.com/w and C++ core guidelines. The last link might be a bit much at the moment, but it does show the current recommendations on how to use C++. As for readabilty, I prefer reading std::string, std::cout so that is just what you are used to. ;) Anyway good luck learning! Commented Dec 26, 2023 at 15:12

2 Answers 2

2

The thing with books is that they don't update themselves, and C++ is constantly evolving. Have a look at the C++ core guidelines, also by Bjarne so you can see how ways of working can change. Here is an idea of what you could do with templates, to not repeat yourself.

#include <format>
#include <iostream>
#include <limits>

// Don't repeat yourself, make functions :)
template<typename type_t>
void show_limits()
{
    std::cout << std::format(
        "{} has a range of [{},{}]\n",
        typeid(type_t).name(),
        std::numeric_limits<type_t>::min(),
        std::numeric_limits<type_t>::max()
    );
}

// char needs a bit of specific formatting 
template<>
void show_limits<char>()
{
    std::cout << std::format(
        "{} has a range of [{:d},{:d}]\n",
        typeid(char).name(),
        std::numeric_limits<char>::min(),
        std::numeric_limits<char>::max()
    );
}

// variadic function template 
template<typename... types_t>
void show_all_limits()
{
    // fold expression
    // will create a call to show_limits for each of the types used.
    ((show_limits<types_t>()), ...);
}

int main()
{
    show_all_limits<char, short, int, long, float, double, long double, unsigned>();
}
Sign up to request clarification or add additional context in comments.

4 Comments

great answer! But I'm getting the following error: * Executing task: C/C++: g++ build active file Starting build... /usr/bin/g++ -fdiagnostics-color=always -g "/Users/maion/CPP/book_The_CPP_Programming_Language_Stroustrup/ch4_ex4.cpp" -o "/Users/maion/CPP/book_The_CPP_Programming_Language_Stroustrup/ch4_ex4" --std=c++20 /Users/maion/CPP/book_The_CPP_Programming_Language_Stroustrup/ch4_ex4.cpp:9:23: error: no member named 'format' in namespace 'std' std::cout << std::format(
You need to enable C++20 , like shown here : godbolt.org/z/eqo8j9Ph5. The format documentation can be found here : en.cppreference.com/w/cpp/utility/format/format. And format is the preferred way of formatting strings from C++20 onward (don't use printf anymore)
it was not that easy since I'm on macOs M1, and clang has no support for format yet (even with -std=c++20 activated). I've installed other compiler and it works now.
Sorry to hear clang gave you so much trouble, 17.0.1 should be able to compile fine (godbolt.org/z/5jhcP3188) . Note I don't have an apple but It should be avialable for M1 too at least that's what I understand from this webpage : dev.to/ayomide_bajo/….
0

As suggested by the wizards (@Iłya Bursov,and @n. m. could be an AI) the problem was that I was displaying the answer in oct (due to another exercise in the same file, not presented in the question, but they nailed it). I had to tell the compiler to display back to dec and no longer oct.

int main(){
// (...)
// Exercise 4.11.4:
    cout << "\nExercise 4.11.4:\n";
    for (int i = 48; i < 127; i++)
    {
        cout
            << char(i) << " Hex:" << hex << i << " Dec:" << dec << i << " Oct:" << oct << i << "" << endl;
    }
    cout << dec << endl; // FIXING TO DEC.
    cout << "\nExercise 4.11.5:\n";

    cout << "Largest and smallest values for different data types:\n";

    // char ???!??!?!?!?!?
    cout << "char: " << static_cast<int>(numeric_limits<char>::min()) << " to "
         << static_cast<int>(numeric_limits<char>::max()) << endl;

    // unsigned char
    cout << "unsigned char: " << static_cast<int>(numeric_limits<unsigned char>::min()) << " to "
         << static_cast<int>(numeric_limits<unsigned char>::max()) << endl;

    // short
    cout << "short: " << numeric_limits<short>::min() << " to "
         << numeric_limits<short>::max() << endl;

    // int
    cout << "int: " << numeric_limits<int>::min() << " to "
         << numeric_limits<int>::max() << endl;

    // float
    cout << "float: " << numeric_limits<float>::lowest() << " to "
         << numeric_limits<float>::max() << endl;

    // double
    cout << "double: " << numeric_limits<double>::lowest() << " to "
         << numeric_limits<double>::max() << endl;

    // long double
    cout << "long double: " << numeric_limits<long double>::lowest() << " to "
         << numeric_limits<long double>::max() << endl;

    // unsigned
    cout << "unsigned: " << 0 << " to "
         << numeric_limits<unsigned>::max() << endl;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.