Problem with <limit>

This is a discussion on Problem with <limit> within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; #include <limits> int main() { cout << "smallest char ==" << numeric_limits<char>::min() << '\n' ...

  1. #1
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55

    Problem with <limit>

    Code:
    #include <iostream>
    using namespace std;
    
    #include <limits>
    
    int main() {
    
      cout << "smallest char ==" << numeric_limits<char>::min() << '\n'
       << "largest char ==" << numeric_limits<char>::max() << '\n'
       << "smallest short ==" << numeric_limits<short>::min() << '\n'
       << "largest short ==" << numeric_limits<short>::max() << '\n';
    
    }
    This code compiles (and doesn't generate warnings with -Wall), but the output is:

    Code:
    smallest char ==
    largest char =
    smallest short ==-32768
    largest short ==32767
    The point of this exercise is to print out the implementation-specific lower and upper bounds of each (primitive) type. What's going on with `char'?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    These are the same:
    Code:
        cout << numeric_limits<char>::min();
        cout << '\0';
    What would you expect to see?

    If you want to see an integer value, cast to an int.

    gg

  3. #3
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55
    These are the same:

    Code:
    cout << numeric_limits<char>::min();
    cout << '\0';What would you expect to see?

    If you want to see an integer value, cast to an int.

    gg
    Not sure if we're on same page

    Idea is to extract lower and upper bounds on `char' for this machine in a representation-independent way.

    I was expecting to see

    smallest char == 0
    largest char == 255

    for an ANSI-compliant implementation.

    Here's the rest:
    Code:
    #include <iostream>
    using namespace std;
    
    #include <limits>
    
    int main() {
    
      cout << "smallest char ==" << numeric_limits<char>::min() << '\n' // Not behaving like others
       << "largest char ==" << numeric_limits<char>::max() << '\n' // Not behaving like others
       << "smallest short ==" << numeric_limits<short>::min() << '\n'
       << "largest short ==" << numeric_limits<short>::max() << '\n'
       << "smallest int ==" << numeric_limits<int>::min() << '\n'
       << "largest int ==" << numeric_limits<int>::max() << '\n'
       << "smallest long ==" << numeric_limits<long>::min() << '\n'
       << "largest long ==" << numeric_limits<long>::max() << '\n'
       << "smallest float ==" << numeric_limits<float>::min() << '\n'
       << "largest float ==" << numeric_limits<float>::max() << '\n'
       << "smallest double ==" << numeric_limits<double>::min() << '\n'
       << "largest double ==" << numeric_limits<double>::max() << '\n'
       << "smallest long double ==" << numeric_limits<long double>::min() << '\n'
       << "largest long double ==" << numeric_limits<long double>::max() << '\n'
       << "smallest unsigned ==" << numeric_limits<unsigned>::min() << '\n'
           << "largest unsigned ==" << numeric_limits<unsigned>::max() << '\n';
    
    
    }
    Output:
    Code:
    smallest char ==
    largest char =
    smallest short ==-32768
    largest short ==32767
    smallest int ==-2147483648
    largest int ==2147483647
    smallest long ==-2147483648
    largest long ==2147483647
    smallest float ==1.17549e-38
    largest float ==3.40282e+38
    smallest double ==2.22507e-308
    largest double ==1.79769e+308
    smallest long double ==3.3621e-4932
    largest long double ==1.18973e+4932
    smallest unsigned ==0
    largest unsigned ==4294967295

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    No, you're not on the same page, slippy. Read his post again. He's given you the answer.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yeah, but not in a very clear way. (And besides, it's wrong in a detail.)

    The C++ I/O streams interpret every value of type char as a character. You want the stream to interpret it as an integer. You'll have to cast it explicitly.
    Code:
    std::cout << static_cast<int>(std::numeric_limits<char>::min()) << '\n';
    You'll also find that the limit is not necessarily 0. The signed-ness of char is implementation-defined. (In fact, those I know use a signed 8-bit char, so the min will actually be -128.) That's why char, signed char and unsigned char are three distinct types.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    Yeah, but not in a very clear way. (And besides, it's wrong in a detail.)

    The C++ I/O streams interpret every value of type char as a character. You want the stream to interpret it as an integer. You'll have to cast it explicitly.
    Code:
    std::cout << static_cast<int>(std::numeric_limits<char>::min()) << '\n';
    You'll also find that the limit is not necessarily 0. The signed-ness of char is implementation-defined. (In fact, those I know use a signed 8-bit char, so the min will actually be -128.) That's why char, signed char and unsigned char are three distinct types.
    But of course, all of zero, 255, 127 and -128 may are likely to be invisible characters [127 is "delete" for example]. I know that 128 is not invisible in Windows [it's C-cedilla in my codepage], but the others are in one way or another.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by CornedBee View Post
    Yeah, but not in a very clear way. (And besides, it's wrong in a detail.)
    The cast is what is important.

  8. #8
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55
    Code:
    std::cout << static_cast<int>(std::numeric_limits<char>::min()) << '\n';
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21