Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Stroustrup Chapter 4, Exercise 5

Reply
Thread Tools

Stroustrup Chapter 4, Exercise 5

 
 
arnuld
Guest
Posts: n/a
 
      03-05-2007
here Stroustrup asks to write the smallest and largest values of char,
int, short, long etc.

i do not understand his question. when i will do

sizeof(char), i wil only get one value in /byte/. where are the
smallest and largest values of /char/

?

 
Reply With Quote
 
 
 
 
mathieu.lacage@gmail.com
Guest
Posts: n/a
 
      03-05-2007
On Mar 5, 4:21 pm, "arnuld" <(E-Mail Removed)> wrote:
> here Stroustrup asks to write the smallest and largest values of char,
> int, short, long etc.
>
> i do not understand his question. when i will do
>
> sizeof(char), i wil only get one value in /byte/. where are the
> smallest and largest values of /char/


I think he means: "the largest value you can store in a variable of
type char".

If char is signed, then the following might calculate the largest
value if you assume that a byte is 8 bits.
(1<<(sizeof (char)*8-1)-1)

Although, generally, you cannot assume that char is signed. (a non-8-
bit byte seems to me of more academic interest)

Mathieu

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      03-05-2007
On Mar 5, 10:21 am, "arnuld" <(E-Mail Removed)> wrote:
> here Stroustrup asks to write the smallest and largest values of char,
> int, short, long etc.
>
> i do not understand his question. when i will do
>
> sizeof(char), i wil only get one value in /byte/. where are the
> smallest and largest values of /char/
>
> ?


He asks what the largest values are *on your system*. Different
systems can have different sizes for these types. Write a little
program that prints out the minimum and maximum value of each type.
You can try to calculate them by hand, or you can just use the
constants in <climits> or the functions that are in <limits>. For
instance:

#include <iostream>
#include <climits>
#include <limits>
using namespace std;

int main()
{
cout << "char's min and max on this system are: "
<< CHAR_MIN << " and " << CHAR_MAX;
cout << "char's min and max on this system are: "
<< numeric_limits<char>::min() << " and "
<< numeric_limits<char>::max();
return 0;
}

Cheers! --M

 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      03-05-2007
arnuld a écrit :
> here Stroustrup asks to write the smallest and largest values of char,
> int, short, long etc.
>
> i do not understand his question. when i will do
>
> sizeof(char), i wil only get one value in /byte/. where are the
> smallest and largest values of /char/ ?


Look for numeric_limits<> templates in STL.
In your case:
numeric_limits<char>::min()
numeric_limits<char>::max()
etc.

Michael

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      03-05-2007
On Mar 5, 10:33 am, (E-Mail Removed) wrote:
> If char is signed, then the following might calculate the largest
> value if you assume that a byte is 8 bits.
> (1<<(sizeof (char)*8-1)-1)
>
> Although, generally, you cannot assume that char is signed. (a non-8-
> bit byte seems to me of more academic interest)


Even if it is mostly an academic issue, I still much prefer to use the
standard macro CHAR_BIT rather than the magic number 8.

Cheers! --M


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stroustrup Chapter 4, Exercise 3 - displaying the sizes of char, int etc. arnuld C++ 0 03-05-2007 03:18 PM
Stroustrup Chapter 4, Exercise 2 arnuld C++ 0 03-05-2007 02:34 PM
Stroustrup: chapter 4 arnuld C++ 13 03-05-2007 07:41 AM
Stroustrup "desk calculator" chapter 6 arnuld C++ 14 11-11-2006 04:14 AM
Stroustrup chapter 3 - 3.6 arnuld C++ 4 11-05-2006 11:25 AM



Advertisments