size of variable types

Thread Starter

mentaaal

Joined Oct 17, 2005
451
Hey guys, quick question:

in my programming notes in college an int is supposed to be 2 bytes and a long double is said to be 10 bytes.

However, when using the sizeof unary operator in visual c++ 2005 express edition, an int is 4 bytes and a long double is 8 bytes.

Could someone please explain?
 

Thread Starter

mentaaal

Joined Oct 17, 2005
451
Ha ha its ok, i just read on the next page:

"Most computers today have two-byte or four-byte integers. Some of the newer machines use eight-byte integers. Because the results of pointer arithmetic depend on the size of the objects a pointer points to, pointer arithmetic is machine dependent."


Thanks anyways!
 

Mark44

Joined Nov 26, 2007
628
Up until about 10 or 12 years ago, many C and C++ compilers for PC-class machines allocated two bytes for an int (and the same for a short), and four bytes for a long. Some compilers had three separate sizes for reals (float - 4 bytes, double - 8 bytes, long double - 10 bytes), and others had just the two smaller sizes. When operating systems changed from 16-bit to 32-bit (when Windows 95 came out), the size of of the int type changed from two bytes to four.

It might seem a bit strange that the size of a basic type would change, but there have never been any definitions in C for how big these types were, only that an int would be the same size as a machine word. When that changed, with 32-bit registers and 32-bit operating systems, the size of an int changed, too.
Mark
 

Papabravo

Joined Feb 24, 2006
21,157
For your compiler environment check out the header file <limits.h> for some insight into the ranges of various declarations.
 
Top