On 30/01/2010, at 9:33 AM, hockchai Lim wrote:

Some how I though long is 8 bytes (64 bits) and int is 4 bytes (32 bits).

Nope, a long long is 8 bytes. An int could be 2 bytes or 4 bytes depending on compiler implementation which in turn depends on the target hardware capabilities.

Generally:
char 1-byte
short int 2-bytes
int 4-bytes
long int 4-bytes
long long 8-bytes

By declaring a long you are informing the compiler you want the longer type (i.e. a 4-byte value) where a plain int might give you 2-bytes on certain C compilers. Even so, the compiler writers could choose to only support 2-byte integers in which case short, int, and long would all give the same allocation.

I wonder what is the reason for long and int types if they both are 4
bytes long.


Historical.

From the K&R book:

:quote.
Plain int objects have the natural size suggested by the host machine architecture; the other sizes are provided to meet special needs. Longer integers provide at least as much storage as short ones, but the implementation may make plain integers equivalent to either short integers or long integers.
:equote.

Thus declaring a plain int could result in different storage allocations (i.e. sizes) on different hardware or even with a different compiler on the same hardware. Much less of an issue now than it once was (indeed now probably non-existent) but was one of the "joys" of porting C code to different platforms.

Regards,
Simon Coulter.
--------------------------------------------------------------------
FlyByNight Software OS/400, i5/OS Technical Specialists

http://www.flybynight.com.au/
Phone: +61 2 6657 8251 Mobile: +61 0411 091 400 /"\
Fax: +61 2 6657 8251 \ /
X
ASCII Ribbon campaign against HTML E-Mail / \
--------------------------------------------------------------------




This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2019 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].