473,772 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FAQ issue: Guaranteed value ranges of fundamental types?

The C++ FAQ item 29.5 (this seems to be strongly related to C), at
<url: http://www.parashift.c om/c++-faq-lite/newbie.html#faq-29.5>
mentions that

<quote>
C++ guarantees a char is exactly one byte which is at least 8 bits, short
is at least 16 bits, int is at least 16 bits, and long is at least 32
bits.
</quote>
Questions:

(1) This guarantee seems to come from the C standard. Which I don't
have. Does the C++ standard really guarantee this?

(2) Is this guarantee originally formulated in terms of number of bits,
or in terms of e.g. decimal value ranges?

(3) Concerning (2), if formulated in terms of number of bits, are the number
of bits mentioned simply sizeof(T)*CHAR_ BIT, which doesn't say much about
value ranges, or are they stated to be the value representation bits?
(Intentionally cross-posted [comp.lang.c++] and [comp.lang.c]).

--
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?
Jul 23 '05 #1
30 1585
Alf P. Steinbach wrote:

The C++ FAQ item 29.5 (this seems to be strongly related to C), at
<url: http://www.parashift.c om/c++-faq-lite/newbie.html#faq-29.5>
mentions that

<quote>
C++ guarantees a char is exactly
one byte which is at least 8 bits, short
is at least 16 bits, int is at least 16 bits,
and long is at least 32 bits.
</quote>

Questions:

(1) This guarantee seems to come from the C standard. Which I don't
have. Does the C++ standard really guarantee this?

(2) Is this guarantee originally
formulated in terms of number of bits,
or in terms of e.g. decimal value ranges?


The part of the C standard which is called
"Sizes of integral types" in C89 and
"Sizes of integer types" in C99,
is specified in terms of ranges.

C99 describes padding bits for integer types.

--
pete
Jul 23 '05 #2
"Alf P. Steinbach" wrote:

The C++ FAQ item 29.5 (this seems to be strongly related to C), at
<url: http://www.parashift.c om/c++-faq-lite/newbie.html#faq-29.5>
mentions that

<quote>
C++ guarantees a char is exactly one byte which is at least 8
bits, short is at least 16 bits, int is at least 16 bits, and
long is at least 32 bits.
</quote>

Questions:

(1) This guarantee seems to come from the C standard. Which I
don't have. Does the C++ standard really guarantee this?
I dunno. This is c.l.c. Google for N869 to get the last draft of
the C standard. f'ups set, which you should have done in the
original posting.

(2) Is this guarantee originally formulated in terms of number of
bits, or in terms of e.g. decimal value ranges?
By values. Other specifications translate that into viable bits.

(3) Concerning (2), if formulated in terms of number of bits, are
the number of bits mentioned simply sizeof(T)*CHAR_ BIT, which
doesn't say much about value ranges, or are they stated to be
the value representation bits?


No, because there may be bits for other purposes, such as trap
values.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Jul 23 '05 #3
* CBFalconer:
"Alf P. Steinbach" wrote:

The C++ FAQ item 29.5 (this seems to be strongly related to C), at
<url: http://www.parashift.c om/c++-faq-lite/newbie.html#faq-29.5>
mentions that

<quote>
C++ guarantees a char is exactly one byte which is at least 8
bits, short is at least 16 bits, int is at least 16 bits, and
long is at least 32 bits.
</quote>

Questions:

(1) This guarantee seems to come from the C standard. Which I
don't have. Does the C++ standard really guarantee this?


I dunno. This is c.l.c. Google for N869 to get the last draft of
the C standard. f'ups set, which you should have done in the
original posting.


It was intentionally cross-posted; F.U.T. overridden. :-)

Thanks for the "N869" reference.

Yes, the C standard specifies those minimum ranges in the documentation
of [limits.h], which the C++ standard refers to the C standard for.

(2) Is this guarantee originally formulated in terms of number of
bits, or in terms of e.g. decimal value ranges?


By values. Other specifications translate that into viable bits.


--
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?
Jul 23 '05 #4
Alf P. Steinbach wrote:
The C++ FAQ item 29.5 (this seems to be strongly related to C), at
<url: http://www.parashift.c om/c++-faq-lite/newbie.html#faq-29.5>
mentions that

<quote>
C++ guarantees a char is exactly one byte which is at least 8 bits, short
is at least 16 bits, int is at least 16 bits, and long is at least 32
bits.
</quote>
Questions:

(1) This guarantee seems to come from the C standard. Which I don't
have. Does the C++ standard really guarantee this?

Yes. Also, except where otherwise is stated, C90 is a subset of C++98
standard.
(2) Is this guarantee originally formulated in terms of number of bits,
or in terms of e.g. decimal value ranges?

Decimal ranges. C90 defines the following (from the last C90 draft):
"Sizes of integral types <limits.h>

The values given below shall be replaced by constant expressions
suitable for use in #if preprocessing directives. Their
implementation-defined values shall be equal or greater in magnitude
(absolute value) to those shown, with the same sign.

* maximum number of bits for smallest object that is not a bit-field
(byte)
CHAR_BIT 8

* minimum value for an object of type signed char
SCHAR_MIN -127

* maximum value for an object of type signed char
SCHAR_MAX +127

* maximum value for an object of type unsigned char
UCHAR_MAX 255

* minimum value for an object of type char
CHAR_MIN see below

* maximum value for an object of type char
CHAR_MAX see below

* maximum number of bytes in a multibyte character, for any
supported locale
MB_LEN_MAX 1

* minimum value for an object of type short int
SHRT_MIN -32767

* maximum value for an object of type short int
SHRT_MAX +32767

* maximum value for an object of type unsigned short int
USHRT_MAX 65535

* minimum value for an object of type int
INT_MIN -32767

* maximum value for an object of type int
INT_MAX +32767

* maximum value for an object of type unsigned int
UINT_MAX 65535

* minimum value for an object of type long int
LONG_MIN -2147483647

* maximum value for an object of type long int
LONG_MAX +2147483647

* maximum value for an object of type unsigned long int
ULONG_MAX 4294967295
If the value of an object of type char sign-extends when used in
an expression, the value of CHAR_MIN shall be the same as that of
SCHAR_MIN and the value of CHAR_MAX shall be the same as that of
SCHAR_MAX . If the value of an object of type char does not sign-extend
when used in an expression, the value of CHAR_MIN shall be 0 and the
value of CHAR_MAX shall be the same as that of UCHAR_MAX./7/"

(3) Concerning (2), if formulated in terms of number of bits, are the number
of bits mentioned simply sizeof(T)*CHAR_ BIT, which doesn't say much about
value ranges, or are they stated to be the value representation bits?

Apart from char, and unsigned char that are guaranteed to not have
padding bits (anyone may tell about signed char?), the number of bits
of a type may contain padding bits etc.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5
Alf P. Steinbach wrote:
[snip]
short is at least 16 bits, int is at least 16 bits, and long
is at least 32 bits.
</quote>


That is contrary to what my copy of the standard says (see 3.9.1/1). It
basically says that char <= short <= int <= long (i.e. there is no
guarantee that a long is any bigger than a char, although there probably
is no platform where sizeof(long) == sizeof(char)). For more information
see 5.3.3 and 1.7..

Regards,

--
Andreas Huber

When replying by private email, please remove the words spam and trap
from the address shown in the header.

Jul 23 '05 #6
"Andreas Huber" <ah************ ********@yahoo. com> wrote...
Alf P. Steinbach wrote:
[snip]
short is at least 16 bits, int is at least 16 bits, and long
is at least 32 bits.
</quote>
That is contrary to what my copy of the standard says (see 3.9.1/1). It
basically says that char <= short <= int <= long (i.e. there is no


I wonder what _exactly_ does your copy say in 3.9.1/1 that makes you
derive that 'char <= short <= int'. But that doesn't matter. You are
actually correct, the C standard defined those relationships between
type sizes. It is true that char <= short <= int. It does not, however,
mean that there is no guarantee that 'int' is larger than 'char'. The
same C90 Standard when describing <limits.h> (and see 18.2.2/2 to know
that C++ mandates the same values for all xx_MAX and xx_MIN values),
does require *at least* ranges -32767..32767 for 'int' and -2^31..2^31
for 'long'.
guarantee that a long is any bigger than a char, although there probably
is no platform where sizeof(long) == sizeof(char)). For more information
see 5.3.3 and 1.7..


For more information see 18.2.2

V
Jul 23 '05 #7
Victor Bazarov wrote:

I wonder what _exactly_ does your copy say in 3.9.1/1 that makes you
derive that 'char <= short <= int'. But that doesn't matter. You are
actually correct, the C standard defined those relationships between
type sizes.


C&V please.
Jul 23 '05 #8
"infobahn" <in******@btint ernet.com> wrote...
Victor Bazarov wrote:

I wonder what _exactly_ does your copy say in 3.9.1/1 that makes you
derive that 'char <= short <= int'. But that doesn't matter. You are
actually correct, the C standard defined those relationships between
type sizes.


C&V please.


I don't have a copy of C90. I used to have a printed copy at work,
but I don't work there any longer. I can be mistaken, therefore.

V
Jul 23 '05 #9
"Andreas Huber" <ah************ ********@yahoo. com> writes:
Alf P. Steinbach wrote:
[snip]
short is at least 16 bits, int is at least 16 bits, and long
is at least 32 bits.
</quote>


That is contrary to what my copy of the standard says (see
3.9.1/1). It basically says that char <= short <= int <= long
(i.e. there is no guarantee that a long is any bigger than a char,
although there probably is no platform where sizeof(long) ==
sizeof(char)). For more information see 5.3.3 and 1.7..


There's no contradiction.

The C standard guarantees that short and int are at least 16 bits, and
long is at least 32 bits (it states these in terms of minimum ranges,
but the requirement for a binary representation implies the sizes
given). It also guarantees that int as at least as wide as short, and
long is at least as wide as int. (Padding bits mean that this applies
to the ranges, not the sizes.)

I think the C++ standard has the same guarantees.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 23 '05 #10

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

Similar topics

15
5961
by: Ladvánszky Károly | last post by:
Entering 3.4 in Python yields 3.3999999999999999. I know it is due to the fact that 3.4 can not be precisely expressed by the powers of 2. Can the float handling rules of the underlying layers be set from Python so that 3.4 yield 3.4? Thanks, Károly
8
1803
by: Kyle Kolander | last post by:
Sorry, I sent this to comp.std.c++ and meant to send it here as well... Why are the minimum size guarantees for fundamental types intentionally omitted from section 3.9.1 Fundamental types of the C++ standard? If indeed these guarantees can be inferred from other parts of the standard, and it is the intent of the standards committee that these guarantees exist, then why are they not listed in the most relevant section? I've been...
2
1724
by: Ben O'Steen | last post by:
Scenario: ========= Using PyGame in particular, I am trying to write an application that will run a scripted timeline of events, eg at 5.5 seconds, play xxx.mp3 and put the image of a ball on screen, at 7.8 seconds move the ball up and down. At this point, I hear you say 'Oh, like Flash'. Yes, well... Like Flash, but I don't want to take this app in the same direction as Macromedia took Flash, nor do I (ever) want the two to be
30
1409
by: Alf P. Steinbach | last post by:
The C++ FAQ item 29.5 (this seems to be strongly related to C), at <url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5> mentions that <quote> C++ guarantees a char is exactly one byte which is at least 8 bits, short is at least 16 bits, int is at least 16 bits, and long is at least 32 bits. </quote>
18
2381
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and compiler), and it is, what I conclude from the below code union data_type {
22
27077
by: subramanian100in | last post by:
Consider the following program #include <limits.h> #include <stddef.h> int main(void) { size_t size; size_t bytes = sizeof(size_t);
14
2411
by: n3o | last post by:
Hello Comp.Lang.C Members, I have an issue with user input that I have been trying to figure out for the longest. For instance, let's say you have something like this: void foo() { int num; printf("Please enter a number: "); scanf("%d", &num); // yes I know this function may throw a
1
1455
by: jehugaleahsa | last post by:
Hello: I am experiencing performance related issues when my custom data structures work with value types. I use generics to prevent boxing wherever I can. For instance, I use IEqualityComparer, etc. I have gone through most of my data structures and verified that I don't compare to null or call methods that would box my value types. However, I am still experiencing performance problems. I can process strings faster than I can process...
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.