Establishing size

Hello, im new to C++ and still learning C++ basics, and i've come across a lesson that teaches 'Establishing size', to data types.
Which include statements such as :
1
2
3
 long int num;
 short int num2;
 unsigned int num3; 


and im just wondering how it would help me in the future >>.
if anyone could help me about this please do. Thanx :)


If I understand your question correctly, you are asking what the difference between those are/when you should use each?

The size of these types depends on the environment. In a 32bit environment, an int will be 32 bits, a short will be half of that. long int is going to be the same size as int. Typically, you don't really need to use a short int unless memory is scarce.
You can make the data type unsigned when whatever it's holding will never go below 0. This also has the effect of doubling the max range that the data type can hold.

If you want to know the size of a type or what its range is, you can just do some test code

 
cout << "size of long: " << sizeof( long ) << endl;


 
std::cout << "Maximum short value: " << std::numeric_limits<short>::max() << std::endl;
Last edited on
Thanx, that helped me understand that much better! :)
Topic archived. No new replies allowed.