473,388 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Stroustrup Chapter 4, Exercise 5

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/

?

Mar 5 '07 #1
4 2055
On Mar 5, 4:21 pm, "arnuld" <geek.arn...@gmail.comwrote:
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

Mar 5 '07 #2
On Mar 5, 10:21 am, "arnuld" <geek.arn...@gmail.comwrote:
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 <climitsor 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

Mar 5 '07 #3
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

Mar 5 '07 #4
On Mar 5, 10:33 am, mathieu.lac...@gmail.com 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
Mar 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I...
11
by: arnuld | last post by:
this is the code which runs without any trouble: ----------------------------------------------------- #include <iostream> #include <string> #include <vector> struct Entry { std::string...
5
by: arnuld | last post by:
i tried but i am not able to think of any solution of this: "find an example where it would make sense to use a name in its own initializer."
0
by: arnuld | last post by:
Stroustrup has a table in section 4.9 of declarations and definitions. he asks to write a similar table but in opposite sense: char ch; // declaration with definition he asks to do the...
0
by: arnuld | last post by:
this programme runs without any trouble. it took 45 minutes of typing. i posted it here so that people can save their precious time: // Stroustrup special edition // chapter 4 exercise 2 //...
2
by: arnuld | last post by:
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 -------------- /*...
6
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
14
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
1
by: blangela | last post by:
Bjarne Stroustrup has a new text coming out called "Programming: Principles and Practice Using C++" (ISBN: 0321543726), due to be published in December of this year. Some of the features of this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.