Possible Duplicate:
How would you set a variable to the largest number possible in C? 开发者_开发技巧
Can we find the maximum size of a data type in C langauge?
If you want to know the maximum and minimum values you can store in a variable of a given data type, you can check with these different constants:
LONG_MIN, LONG_MAX, see here, for the rest.
There is no maximum size. A data type has a size, and it remains constant in that implementation. You can get it by sizeof(datatype)
.
If you ask for the maximal number representable by a data type, then for unsigned types you can just do (unsigned type)-1
. This is useful if you just use an unsigned typedef (size_t
etc) and don't know the exact underlying type name. For signed types, this won't work. There are macros for this though (including the unsigned variants)
INT_MAX /* maximal int value */
LONG_MAX /* maximal long value */
UINT_MAX /* maximal unsigned int value */
/* etc... */
精彩评论