By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,687 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

Size of long

P: n/a
I know that C++ Standard does not impose any requirements for the size of
_long_ type. I wonder what is the size of _long_ in reality? I don't think
that there are architectures with 1-byte _long_. I think, the most probable
size for _long_ would be 4 bytes. What about 2-byte _long_, are there any
architectures with that size?

Thanks,

Nikolai Borissov
Jul 19 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Nikolai Borissov wrote:
I know that C++ Standard does not impose any requirements for the size of
_long_ type. I wonder what is the size of _long_ in reality?


What do you mean "in reality"? that depends on the implementation /
system. Just do a cout << sizeof(long); and find out for your system.
for my system, long is 4bytes.

-- Nuclear / the Lab --

Jul 19 '05 #2

P: n/a

"Nikolai Borissov" <n.********@sympatico.ca> skrev i melding
news:J9*******************@news20.bellglobal.com.. .
I know that C++ Standard does not impose any requirements for the size of
_long_ type. I wonder what is the size of _long_ in reality? I don't think
that there are architectures with 1-byte _long_. I think, the most probable size for _long_ would be 4 bytes. What about 2-byte _long_, are there any
architectures with that size?

Thanks,

Nikolai Borissov


AFAIK you have no guarantee on the size of the long type in your case, what
is guaranteed is the minimum size of this type, wich is 4 bytes for the long
type. The int type is 2 bytes on some systems and 4 on others, you are
however guaranteed that it is at the minimum 2 bytes.

Could someone confirm this ?

-Magnus
Jul 19 '05 #3

P: n/a
Magnus wrote:
AFAIK you have no guarantee on the size of the long type in your case, what is guaranteed is the minimum size of this type, wich is 4 bytes for the long type. The int type is 2 bytes on some systems and 4 on others, you are
however guaranteed that it is at the minimum 2 bytes.
If minimum of 4 bytes is guaranteed by the Standard it would be excellent.
I've got only Draft Standard and couldn't find any specific information
about _long_ type. This whole point about portability.
Could someone confirm this ?


Someone who is the true expert in the Standard, please respond.

Nikolai Borissov
Jul 19 '05 #4

P: n/a
Nikolai Borissov wrote:
Magnus wrote:

AFAIK you have no guarantee on the size of the long type in your case,


what
is guaranteed is the minimum size of this type, wich is 4 bytes for the


long
type. The int type is 2 bytes on some systems and 4 on others, you are
however guaranteed that it is at the minimum 2 bytes.

If minimum of 4 bytes is guaranteed by the Standard it would be excellent.
I've got only Draft Standard and couldn't find any specific information
about _long_ type. This whole point about portability.

Could someone confirm this ?

Someone who is the true expert in the Standard, please respond.


I don't claim to be a "true expert" in the C/C++ standards, but...

The 'int' type must be able to represent values in the range INT_MIN to
INT_MAX, as defined in the header <climits> (see: 3.9.1/2 and footnote
39). The 'long int' type must provide *at least* as much storage as the
'int' type (3.9.1/2).

According to the C(99) standard [which is NOT the same C standard that
C++ uses], the implementation-defined INT_MIN and INT_MAX values must be
equal or greater in magnitude (absolute value) to the following values,
with the same sign:

INT_MIN -32767
INT_MAX +32767

So assuming 8-bit bytes, and assuming the C(90) INT_MAX, INT_MIN
definitions are essentially the same as the C(99) definitions, then C++
guarantees,

2 <= sizeof(int) <= sizeof(long int)

IOW, the guaranteed minimum size of a 'long int' object is 2 bytes, not
4. At least, this is how I read the C(99)/C++(98) standards.

FWIW, on 32-bit platforms, a "typical" standard C library implementation
defines INT_MIN and INT_MAX so that (assuming 8-bit bytes),

4 <= sizeof(int) <= sizeof(long int)

But these are only "typical" values; they are not guaranteed values.

Comments from the real C/C++ standard experts are welcome...

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #5

P: n/a

Nikolai Borissov <n.********@sympatico.ca> wrote in message
news:J9*******************@news20.bellglobal.com.. .
I know that C++ Standard does not impose any requirements for the size of
_long_ type.
No you don't know that, since it's not true.
Type 'long' is required to occupy at least 32 bits
(but is allowed to be larger).
I wonder what is the size of _long_ in reality?
It's different among implementations. You can use
the 'sizeof' operator to determine the size of a
particular type.

sizeof(long)
I don't think
that there are architectures with 1-byte _long_.
There indeed could be. Note that the size of a byte
(char) is required to be at least eight bits, but
could be more (e.g. 32 bits), in which case a 'long'
might have size of one byte (but might not :-))
I think, the most probable
size for _long_ would be 4 bytes.
That's a common size for platforms with eight bit bytes.
What about 2-byte _long_, are there any
architectures with that size?


There could be if the host platform used 16-bit bytes.

Note that you can use the macros from <climits> or
member functions of 'std::numeric_limits' from <limits>
to determine the smallest/largest possible values for
specific types on your implementation.

-Mike

Jul 19 '05 #6

P: n/a

Magnus <no****@forme.com> wrote in message
news:3f********@news.broadpark.no...

"Nikolai Borissov" <n.********@sympatico.ca> skrev i melding
news:J9*******************@news20.bellglobal.com.. .
I know that C++ Standard does not impose any requirements for the size of _long_ type. I wonder what is the size of _long_ in reality? I don't think that there are architectures with 1-byte _long_. I think, the most probable
size for _long_ would be 4 bytes. What about 2-byte _long_, are there any architectures with that size?

Thanks,

Nikolai Borissov


AFAIK you have no guarantee on the size of the long type in your case,

what is guaranteed is the minimum size of this type, wich is 4 bytes for the long type. The int type is 2 bytes on some systems and 4 on others, you are
however guaranteed that it is at the minimum 2 bytes.

Could someone confirm this ?


I can 'disconfirm' it. :-)

There's no requirement that the size of a byte not
be more than eight bits. E.g. a platform with sixteen
bit bytes can have a conforming type 'long' which is
only two bytes in size.

-Mike

Jul 19 '05 #7

P: n/a

Nikolai Borissov <n.********@sympatico.ca> wrote in message
news:R7*******************@news20.bellglobal.com.. .
Magnus wrote:
AFAIK you have no guarantee on the size of the long type in your case, what
is guaranteed is the minimum size of this type, wich is 4 bytes for the

long
type. The int type is 2 bytes on some systems and 4 on others, you are
however guaranteed that it is at the minimum 2 bytes.


If minimum of 4 bytes is guaranteed by the Standard it would be excellent.


First, drop the assumption that all bytes are eight bits.
They're not. The standard does require a byte have
*at least* eight bits, but allows more.

The smallest allowed size for type 'long' is sixteen bits.
However many bytes that takes on the host platform, depends
upon that platform.
I've got only Draft Standard and couldn't find any specific information
about _long_ type. This whole point about portability.
I think you need to look harder. :-)
All the types' requirements are specified.
Could someone confirm this ?

Nope. Can't confirm what ain't true. :-)


Someone who is the true expert in the Standard, please respond.


No such person exists imo.

-Mike

Jul 19 '05 #8

P: n/a
On Thu, 3 Jul 2003 19:41:02 -0700, "Nikolai Borissov"
<n.********@sympatico.ca> wrote in comp.lang.c++:
I know that C++ Standard does not impose any requirements for the size of
_long_ type. I wonder what is the size of _long_ in reality? I don't think
that there are architectures with 1-byte _long_. I think, the most probable
size for _long_ would be 4 bytes. What about 2-byte _long_, are there any
architectures with that size?

Thanks,

Nikolai Borissov


Sigh, another desk-top chauvinist. You think the platforms you work
on are all there is, whereas C and C++ are quite extensively used in
embedded systems using processor architectures you have never heard
of.

I am literally writing code for a Texas Instruments 2812 16/32 bit
digital signal processor which comes with (subset) free-standing C and
C++ compilers.

This processor can't access memory in anything smaller than 16 bit
words. So the character types are 1 byte (but have 16 bits). The
short and int types are also 1 byte (with 16 bits). And signed and
unsigned long are 2 bytes long and have 32 bits.

And there are 32 bit DSPs (from TI, AD, and probably Motorola) where
everything is 32 bits, char, short, int, and long. So sizeof(long) ==
1.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #9

P: n/a
Jim Fischer wrote:

According to the C(99) standard [which is NOT the same C standard that
C++ uses], the implementation-defined INT_MIN and INT_MAX values must be
equal or greater in magnitude (absolute value) to the following values,
with the same sign:

INT_MIN -32767
INT_MAX +32767

So assuming 8-bit bytes, and assuming the C(90) INT_MAX, INT_MIN
definitions are essentially the same as the C(99) definitions, then C++
guarantees,


That was true in the C90 standard, so it applies in C++ as well. But
there's something even more useful: LONG_MIN and LONG_MAX are also
specified, and impose a minimum size of 32 bits.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #10

P: n/a
In article <J9*******************@news20.bellglobal.com>,
n.********@sympatico.ca says...
I know that C++ Standard does not impose any requirements for the size of
_long_ type. I wonder what is the size of _long_ in reality? I don't think
that there are architectures with 1-byte _long_. I think, the most probable
size for _long_ would be 4 bytes. What about 2-byte _long_, are there any
architectures with that size?


A long must be a minimum of 32 bits. A byte must be a minimum of 8
bits, but may be more, so if long is 32 bits, it can be anywhere from 1
to 4 bytes. If long is larger than, more bits can be more bytes (still
subject to the 8 bits per byte minimum) so, for example, a 64-bit long
could be anywhere from 1 to 8 bytes.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #11

P: n/a

Rolf Magnus <ra******@t-online.de> wrote in message
news:be*************@news.t-online.com...
Mike Wahler wrote:
The smallest allowed size for type 'long' is sixteen bits.


No, it's 32.


Um, yes, that's what I meant to say (see my other posts
this thread). Sorry for the typo.

-Mike

Jul 19 '05 #12

P: n/a
Pete Becker wrote:
Jim Fischer wrote:
According to the C(99) standard [which is NOT the same C standard that
C++ uses], the implementation-defined INT_MIN and INT_MAX values must be
equal or greater in magnitude (absolute value) to the following values,
with the same sign:

INT_MIN -32767
INT_MAX +32767

So assuming 8-bit bytes, and assuming the C(90) INT_MAX, INT_MIN
definitions are essentially the same as the C(99) definitions, then C++
guarantees,

That was true in the C90 standard, so it applies in C++ as well. But
there's something even more useful: LONG_MIN and LONG_MAX are also
specified, and impose a minimum size of 32 bits.


So what is the connection between the C++ standard and the LONG_MIN,
LONG_MAX values in the C standard? What I mean is, the C++ standard
specifically mentions C's INT_MIN, INT_MAX values WRT C++'s 'int' type
(see 3.9.1/2 and footnote 39). Does the C++ standard impose a similar
requirement that the C++ 'long int' type be able to represent (at
least?) the same range of values as the C language's LONG_MIN, LONG_MAX
values?

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #13

P: n/a
Pete Becker wrote:
Jim Fischer wrote:
So what is the connection between the C++ standard and the LONG_MIN,
LONG_MAX values in the C standard? What I mean is, the C++ standard
specifically mentions C's INT_MIN, INT_MAX values WRT C++'s 'int' type
(see 3.9.1/2 and footnote 39). Does the C++ standard impose a similar
requirement that the C++ 'long int' type be able to represent (at
least?) the same range of values as the C language's LONG_MIN, LONG_MAX
values?

See 18.2.2/1 [lib.c.limits], which incorporates the C limits macros
through <climits>. It incorporates the indirection that's in the C
standard: the macros specify the actual range of representable values,
along with minimum requirements that must be met.


Ah, of course. <forehead smack> Thanks, Pete.

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #14

P: n/a
On Fri, 4 Jul 2003 04:11:22 -0700, "Nikolai Borissov"
<n.********@sympatico.ca> wrote in comp.lang.c++:
Jack Klein wrote:
Sigh, another desk-top chauvinist. You think the platforms you work
on are all there is, whereas C and C++ are quite extensively used in
embedded systems using processor architectures you have never heard
of.


Well, I'm not a desk-top chauvinist, otherwise I wouldn't have bothered at
all.
I am literally writing code for a Texas Instruments 2812 16/32 bit
digital signal processor which comes with (subset) free-standing C and
C++ compilers.

This processor can't access memory in anything smaller than 16 bit
words. So the character types are 1 byte (but have 16 bits). The
short and int types are also 1 byte (with 16 bits). And signed and
unsigned long are 2 bytes long and have 32 bits.

And there are 32 bit DSPs (from TI, AD, and probably Motorola) where
everything is 32 bits, char, short, int, and long. So sizeof(long) ==
1.


Actually, I was a bit inaccurate in my original question, because what I'm
concerned with is not a _sizeof_ value of type _long_, rather number of bits
in the type. Most of the calculations in my program require only 16-bit size
variables, but in some cases 32-bit ones are necessary. For 16-bit
calculations I use type _int_, for 32-bit type _long_, even though they are
of the same 32-bit size on my desktop. I'm deliberately using type _long_
with the portability concern in mind. The good news in your post is that the
2 imbedded processors you mentioned do have 32-bit long.

Cheers,
Nikolai Borissov


One possibility is to use a version of the latest C standard's
<inttypes.h>, which is likely to make it into the next update to the
C++ standard.

Almost all platforms have integer types that meet the requirements
(with the exception of those DSPs I mentioned above), so you can
tailor it accordingly.

That way you can use int16_t when you want a value that can hold 16
bits, and int32_t when you need more range.

On a typical 32 bit compiler, int32_t would be a typedef for int, on a
16 bit compiler, a typedef for long.

Likewise int16_t would be a typedef for int on a compiler with 16 bit
ints, and for short on a typical 32 bit desk top compiler.

The C++ standard specifies the minimum ranges of values that each of
the integer types must hold, and it inherits those values from C.
Given that a pure binary notation is required for the integer types,
it is easy to work out the minimum number of bits each type must
contain:

8 bits for the character types

16 bits for short and int

32 bits for long

64 bits for long long (another C99 feature likely to make in into
the next update of the C++ standard)

So on any conforming C or C++ implementation, long must have at least
32 bits.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #15

P: n/a
Jack Klein wrote:
One possibility is to use a version of the latest C standard's
<inttypes.h>, which is likely to make it into the next update to the
C++ standard.

Almost all platforms have integer types that meet the requirements
(with the exception of those DSPs I mentioned above), so you can
tailor it accordingly.
Well, only the fixed size types might not be present.
That way you can use int16_t when you want a value that can hold 16
bits, and int32_t when you need more range.


I'd rather use int_fast16_t or int_least16_t if I need not more than 16
bits, unless there is a really good reason to need _exactly_ 16 bits.
This will also work on the DSPs you mentioned (since those are also
defined if there is no type with exactly 16 bits), and it should,
depending on which you choose, use the smallest possible or the fastest
possible type with at least 16 bits. So if you want to create a huge
array and save memory, you can choose the int_least16_t, and for single
values, int_fast16_t.
Jul 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.