Stroustrup Chapter 4, Exercise 5

A

arnuld

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/

?
 
M

mathieu.lacage

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
 
M

mlimber

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
 
M

Michael DOUBEZ

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
 
M

mlimber

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top