Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > difference between int and long

Reply
Thread Tools

difference between int and long

 
 
Imran
Guest
Posts: n/a
 
      01-16-2007
hello all

I am bit confused with int and long data type.

I read in some book that, int cal hold 2^16 values
where in long can hold 2^32. and both data types are 4 bytes long.

My doubt this, since both are 4 bytes , how can it hold different size?

Thank you


 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      01-16-2007

Imran napsal:
> hello all
>
> I am bit confused with int and long data type.
>
> I read in some book that, int cal hold 2^16 values
> where in long can hold 2^32. and both data types are 4 bytes long.
>
> My doubt this, since both are 4 bytes , how can it hold different size?
>
> Thank you


Standard guarantees only that int is at least 16-bit and long is at
least 32-bit and sizeof(int) <= sizeof(long).

So for example on typical 32-bit platform are int and long both 32-bit,
on 64-bit Linux can be int 32-bit and long 64-bit etc.

 
Reply With Quote
 
 
 
 
Scott McPhillips [MVP]
Guest
Posts: n/a
 
      01-16-2007
Imran wrote:
> hello all
>
> I am bit confused with int and long data type.
>
> I read in some book that, int cal hold 2^16 values
> where in long can hold 2^32. and both data types are 4 bytes long.
>
> My doubt this, since both are 4 bytes , how can it hold different size?
>
> Thank you
>
>


If they are both 4 bytes then they can both hold 2^32. The difference
between int and long depends on the type of processor. On many
processors today they are exactly the same.

--
Scott McPhillips [VC++ MVP]

 
Reply With Quote
 
Philipp Reh
Guest
Posts: n/a
 
      01-16-2007
On Tue, 16 Jan 2007 10:30:40 -0500, Scott McPhillips [MVP] wrote:

> Imran wrote:
>> hello all
>>
>> I am bit confused with int and long data type.
>>
>> I read in some book that, int cal hold 2^16 values
>> where in long can hold 2^32. and both data types are 4 bytes long.
>>
>> My doubt this, since both are 4 bytes , how can it hold different size?
>>
>> Thank you
>>
>>

>
> If they are both 4 bytes then they can both hold 2^32. The difference
> between int and long depends on the type of processor. On many
> processors today they are exactly the same.
>


That is not true. C++ defines a byte in terms of the size of a character
which must be at least 8 bits.
The meaning of "4 bytes" thus is 4 times the size of char which doesn't
have to equal 32 bits in size.
 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-17-2007
Philipp Reh write:

> > If they are both 4 bytes then they can both hold 2^32. The difference
> > between int and long depends on the type of processor. On many
> > processors today they are exactly the same.
> >

>
> That is not true. C++ defines a byte in terms of the size of a character
> which must be at least 8 bits.
> The meaning of "4 bytes" thus is 4 times the size of char which doesn't
> have to equal 32 bits in size.


It seems to me, that there is no keyword "byte" in C++ programs.
Outside of C++, most people treat bit as bit and byte as 8 bit - fixed
size of "byte" is more useful, because a man can understand concrete
size of memory in bits. I think "byte" is language independent system
independent memory size, similar to "bit".

But C++ "char" is not a "byte". C++ "char" is minimal system depended
granularity (part) of memory,
a) char by char describe continuous memory ( memory without holes and
lost bits )
b) char is suitable for all other types
1) as align bound of the type
2) number of chars per the type is integral number (not float)
c) sizeof() of char is always "1"
d) each type has MAX_SIZE according only to memory size - if "char" is
16 bit, char can hold values 0x0000 - 0xffff, not 0x00 - 0xff.
e) 8bit<=char<=short<=int<=long

On x86 "char" number of "bits" is equal to "byte" number of "bits".

The reason to declare type with sizeof==2, aligned in memory to
sizeof==4 boundary is allow packed storage in external memory or packed
structures, aligned to sizeof==2.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-17-2007
* Grizlyk:
> Philipp Reh write:
>
>>> If they are both 4 bytes then they can both hold 2^32. The difference
>>> between int and long depends on the type of processor. On many
>>> processors today they are exactly the same.
>>>

>> That is not true. C++ defines a byte in terms of the size of a character
>> which must be at least 8 bits.
>> The meaning of "4 bytes" thus is 4 times the size of char which doesn't
>> have to equal 32 bits in size.

>
> It seems to me, that there is no keyword "byte" in C++ programs.


That's right, but in the C++ standard and C++ context (or C, for that
matter) 'byte' means 'char' and vice versa.


> Outside of C++, most people treat bit as bit and byte as 8 bit - fixed
> size of "byte" is more useful, because a man can understand concrete
> size of memory in bits.


Yep, that's the old "eat dung" argument, that twenty zillion flies can't
be wrong. Yet, while they're undoubtedly doing what's right for them,
and so aren't wrong about eating that dung, the inference that the same
behavior is appropriate for, say, humans, is incorrect. There's more
than one meaning of 'byte' -- after all, language evolves -- and in a
technical context where the number of bits matters the 8 bit entity is
more properly and precisely called an 'octet' (mostly this is used by
electrical engineers and the telecommunications/network industry).


> I think "byte" is language independent system
> independent memory size, similar to "bit".


Not in the context of C++. Look it up at NIST, IEC or really anywhere.
Outside the context of C++, it is a /system dependent/ size. Which is
why 'octet' is used when it's necessary to be precise.


> But C++ "char" is not a "byte". C++ "char" is minimal system depended
> granularity (part) of memory,


Sorry, that's incorrect. See 1.7/1 (byte defined as fundamental
storage unit), 3.9/2 (effective equivalence of bytes and chars wrt.
copying PODs) and 5.3.3/1 (sizeof yields the number of bytes, sizeof
char is 1).

--
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?
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-17-2007

"Grizlyk" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Philipp Reh write:
>
>> > If they are both 4 bytes then they can both hold 2^32. The difference
>> > between int and long depends on the type of processor. On many
>> > processors today they are exactly the same.
>> >

>>
>> That is not true. C++ defines a byte in terms of the size of a character
>> which must be at least 8 bits.
>> The meaning of "4 bytes" thus is 4 times the size of char which doesn't
>> have to equal 32 bits in size.

>
> It seems to me, that there is no keyword "byte" in C++ programs.


No, the keyword is 'char'. The standard (IEC 14882) defines 'char'
as being a byte.

> Outside of C++, most people treat bit as bit


A bit is a bit.

>and byte as 8 bit - fixed


That's not always true. The proper term for an eight-bit object
is 'octet'.

> size of "byte" is more useful, because a man can understand concrete
> size of memory in bits.


Except that each platform defines what 'concrete' is.

> I think "byte" is language independent system


It's platform dependent. Some use different size bytes than others.
that's why we have the 'CHAR_BIT' macro (and
std::numeric_limits<char>::digits)

> independent memory size, similar to "bit".
>
> But C++ "char" is not a "byte". C++ "char" is minimal system depended
> granularity (part) of memory,


Both C and C++ define 'char' as meaning *exactly* 'byte', whatever
the size of a byte on the host platform happens to be. However, both
languages require that the size of a char (i.e. byte) be at least eight
bits.

So on any system, regardless of how many bits in a byte, sizeof(char) == 1.
Always.

-Mike


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-17-2007

Alf P. Steinbach wrote:

> Yep, that's the old "eat dung" argument, that twenty zillion flies can't


No, it is not "eat dung". Ii is standard international (SI) measure of
information or storage capacity, used by most people in practical
cases, similarly to meter or second.

> > But C++ "char" is not a "byte". C++ "char" is minimal system depended
> > granularity (part) of memory,

>
> Sorry, that's incorrect. See 1.7/1 (byte defined as fundamental
> storage unit), 3.9/2 (effective equivalence of bytes and chars wrt.
> copying PODs) and 5.3.3/1 (sizeof yields the number of bytes, sizeof
> char is 1).


You are right formally, but fortunatelly, the part of standard can be
silently ignored at practical cases, as well as we can silently ignore
existence of keyword "goto". I am shure, if you will describe storage
capacity elsewere out of C++ context, much better for all (and for you
too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
"13-bit C++ bytes", if you want, that your opponent can understand what
did you say.

And fortunatelly, "byte" is not C++ keyword, so we can easy use "char"
- C++ specific memory size as base for all types.

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-17-2007
Mike Wahler

See here:
http://groups.google.com/group/comp....7d9cc0c2722964

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-17-2007
* Grizlyk:
> Alf P. Steinbach wrote:
>
>> Yep, that's the old "eat dung" argument, that twenty zillion flies can't

>
> No, it is not "eat dung". Ii is standard international (SI) measure of
> information or storage capacity, used by most people in practical
> cases, similarly to meter or second.
>
>>> But C++ "char" is not a "byte". C++ "char" is minimal system depended
>>> granularity (part) of memory,

>> Sorry, that's incorrect. See 1.7/1 (byte defined as fundamental
>> storage unit), 3.9/2 (effective equivalence of bytes and chars wrt.
>> copying PODs) and 5.3.3/1 (sizeof yields the number of bytes, sizeof
>> char is 1).

>
> You are right formally, but fortunatelly, the part of standard can be
> silently ignored at practical cases, as well as we can silently ignore
> existence of keyword "goto". I am shure, if you will describe storage
> capacity elsewere out of C++ context, much better for all (and for you
> too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
> "13-bit C++ bytes", if you want, that your opponent can understand what
> did you say.
>
> And fortunatelly, "byte" is not C++ keyword, so we can easy use "char"
> - C++ specific memory size as base for all types.


Manual Plonk.

--
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Promoting unsigned long int to long int pereges C Programming 112 07-28-2008 05:00 AM
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments