473,788 Members | 3,078 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?
Nov 14 '05 #1
30 1410
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
Nov 14 '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
Nov 14 '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?
Nov 14 '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
Nov 14 '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.

Nov 14 '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
Nov 14 '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.
Nov 14 '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
Nov 14 '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.
Nov 14 '05 #10

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

Similar topics

15
5963
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
30
1590
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>
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
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
9655
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
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
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
10110
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
6749
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
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.