Advertisement

C++ Datatypes

Started by March 26, 2002 11:10 AM
7 comments, last by steveharper101 22 years, 11 months ago
I know that every compiler has different sizes for different data types. Does anybody know the sizes in bytes of: short int long int unsigned int signed int unsigned short int unsinged long int In Microsoft Visual C++ 6. I have checked the documentation in MSDN but canot find much info on this. Thanks for the help :-) ~Steve~
quote:
Original post by steveharper101
I know that every compiler has different sizes for different data types.

Incorrect. Data type sizes are defined by hardware platform. The only guarantee in C++ is that:
sizeof(char) < sizeof(short int) < sizeof(int) <= sizeof(long int) <= sizeof(float) < sizeof(double).

For Win32:
short int           16long  int           32unsigned int        32signed int          32unsigned short int  16unsinged long int   32 


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
Advertisement
Just do this


  #include <iostream>using namespace std;void main(){   cout<<"The size of \n"       <<"short int is "<<sizeof(short int)<<" bytes \n"       <<"long int is "<<sizeof(long int)<<" bytes \n"       <<"unsigned int is "<<sizeof(unsigned int)<<" bytes \n"       <<"signed int is "<<sizeof(signed int)<<" bytes \n"       <<"unsigned short int is "<<sizeof(unsigned short int)<<" bytes \n"       <<"unsigned long int is "<<sizeof(unsigned long int)<<" bytes \n";       }  


sizeof() is handy to know about.

Regards.
Why make it simple when you can make it sooo nice and complicated?
quote:

sizeof(long int) <= sizeof(float)

Not sure about any guarantee of that.
But for all other comparisons I agree.
How come long integers and integers are the same!? Doesn''t that defeat the whole purpose of using "long?"

/me punches computer



--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."
I would add that although knowing the sizes of the various datatypes is very useful information, you should never explicitly put a concrete size in your program. Instead you should just use the sizeof() operator any time you''d need the size of a particular type.

The reason for this is that if you just use sizeof() in every case, it is much less likely that your program will crash and burn if you are trying to migrate to a system with different sizes for the various types.

-D
Advertisement
quote:
Original post by Matt Calabrese
How come long integers and integers are the same!? Doesn''t that defeat the whole purpose of using "long?"



They are currently the same because int defaults to long, it depends on you compiler. Don''t count on defaults though. As soon as we will start using 64 bit operating systems you''d probably have int default to int64 (in new compilers).

Why make it simple when you can make it sooo nice and complicated?
Output from the above program on Tru64:

The size of
short int is 2 bytes
long int is 8 bytes
unsigned int is 4 bytes
signed int is 4 bytes
unsigned short int is 2 bytes
unsigned long int is 8 bytes

See, long *is* useful
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Matt Calabrese: on 16-bit systems such as MS-DOS an "int == 16-bits" and "long == 32-bits". it''s really, old school and probably a moot point now.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

This topic is closed to new replies.

Advertisement