Stroustrup 4.11 exercise 5

Discussion in 'C++' started by arnuld, Mar 26, 2007.

  1. arnuld

    arnuld Guest

    MAX and MIN values of CHAR could not be displayed. Why ?

    BTW, any advice on improvement ?

    (please remember i have covered chapter 4 only)

    ------------- PROGRAMME --------------
    /* Stroustrup 3e, section 4.11, exercise 5

    STATEMENT:
    what, on your system, are the largest and smallest values of the
    following types:
    char, short, int, long, float, double, long double and unsigned.

    */


    #include<iostream>
    #include<limits>

    int main()
    {
    std::cout << "Largest float == " <<
    std::numeric_limits<float>::max() << '\n';
    std::cout << "Smallest float == " <<
    std::numeric_limits<float>::min() << '\n';
    std::cout << "Largest double == " <<
    std::numeric_limits<double>::max() << '\n';
    std::cout << "Smallest double == " <<
    std::numeric_limits<double>::min() << '\n';
    std::cout << "Largest long double == " <<
    std::numeric_limits<long double>::max() << '\n';
    std::cout << "smallest long double == " <<
    std::numeric_limits<long double>::min() << '\n';

    std::cout << '\n';
    std::cout << "Largest char == " <<
    std::numeric_limits<char>::max() << '\n';
    std::cout << "Smallest char == " <<
    std::numeric_limits<char>::min() << '\n';
    std::cout << "Largest int == " <<
    std::numeric_limits<int>::max() << '\n';
    std::cout << "Smallest int == " <<
    std::numeric_limits<int>::min() << '\n';
    std::cout << "Largest short == " <<
    std::numeric_limits<short>::max() << '\n';
    std::cout << "Smallest short == " <<
    std::numeric_limits<short>::min() << '\n';

    std::cout << '\n';
    std::cout << "Largest Unsigned (int) == " <<
    std::numeric_limits<unsigned>::max() << '\n';
    std::cout << "Smallest Unsigned (int) == " <<
    std::numeric_limits<unsigned>::min() << '\n';
    std::cout << "Largest Long (int) == " <<
    std::numeric_limits<long>::max() << '\n';
    std::cout << "Smallest Long (int) == " <<
    std::numeric_limits<long>::min() << '\n';

    return 0;
    }
    ------------- OUTPUT -------------
    [arch@voodo tc++pl]$ g++ -pedantic -ansi -Wall -Wextra 4.11_5.cpp
    [arch@voodo tc++pl]$ ./a.out
    Largest float == 3.40282e+38
    Smallest float == 1.17549e-38
    Largest double == 1.79769e+308
    Smallest double == 2.22507e-308
    Largest long double == 1.18973e+4932
    smallest long double == 3.3621e-4932

    Largest char == 
    Smallest char == ?
    Largest int == 2147483647
    Smallest int == -2147483648
    Largest short == 32767
    Smallest short == -32768

    Largest Unsigned (int) == 4294967295
    Smallest Unsigned (int) == 0
    Largest Long (int) == 2147483647
    Smallest Long (int) == -2147483648
    [arch@voodo tc++pl]$
     
    arnuld, Mar 26, 2007
    #1
    1. Advertising

  2. * arnuld:
    > MAX and MIN values of CHAR could not be displayed. Why ?


    std::cout interprets those values as characters, because the type is 'char'.

    To have them displayed as numbers, just add 0 to force a promotion to 'int'.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 26, 2007
    #2
    1. Advertising

  3. arnuld

    arnuld Guest

    > On Mar 26, 3:53 pm, "Alf P. Steinbach" <> wrote:

    > std::cout interprets those values as characters, because the type is 'char'.
    >
    > To have them displayed as numbers, just add 0 to force a promotion to 'int'.


    cool... :cool:
     
    arnuld, Mar 26, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. arnuld

    Stroustrup 5.9 exercise 6

    arnuld, Nov 8, 2006, in forum: C++
    Replies:
    7
    Views:
    373
    arnuld
    Nov 8, 2006
  2. arnuld
    Replies:
    5
    Views:
    345
    Alf P. Steinbach
    Nov 8, 2006
  3. arnuld
    Replies:
    2
    Views:
    297
    arnuld
    Nov 8, 2006
  4. arnuld
    Replies:
    6
    Views:
    385
    arnuld
    Nov 8, 2006
  5. arnuld

    Stroustrup 5.9 exercise 9

    arnuld, Nov 8, 2006, in forum: C++
    Replies:
    5
    Views:
    394
    Bernd Strieder
    Nov 9, 2006
Loading...

Share This Page