Public Group

# Relationship of size of variable, operation system and maximum and var can hold

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there a forumla or something that tells me the maximum a variable can hold on a certain operating system. My programming in C book doesn't really explain it that well. Like say i have a short int which is 2 bytes. And im using Windows 32. What would be the maxiumum the variable can hold. Thanks for any help.

##### Share on other sites
#include <climits> //or if using C instead of C++ ....
#include <limits.h>

##### Share on other sites
Well, yeah, there is a formula. And that would be 2bytes*8 for unsigned integral numbers or 2bytes*8-1 for signed.. Though the formula doesn't fit for a bool, even though it's 1 byte in size.

I think mgarriss was referring to the STL numeric_limits template class for C++. Which you could use thusly:

#include <limits>cout << "int max: " << std::numeric_limits<int>::max() << endl;cout << "short max: " << std::numeric_limits<short>::max() << endl;

You can find a full listing of the numeric_limits class methods
• yea thanks for the help.. I was just looking for that forumla. Although I cant get the signed formula to work. Also is 8 just a random number or does that have a significance..

##### Share on other sites
8 is the number of bits in a byte... usually. A more accurate formula is:

2^(bits)

##### Share on other sites
the AP was me: when I say 'usually' I mean that it's not required to be 8 bits a byte. A byte is the size of a char in c/c++ by the standard and the char just has to be a minimum size and that's it. So a byte could be 20 bits, or 10, or 9.... that's why using the standard limits is a better idea because it's not safe to assume.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 15
• 13
• 14
• 75
• 22
• ### Forum Statistics

• Total Topics
632145
• Total Posts
3004345

×