Is the maximum integer value of signed integer types guaranteed to be(uintmax_t)pow(2, sizeof(TYPE) * CHAR_BIT - 1) - 1
in C and in C++?
No.
Integer types are permitted to have padding bits which do not contribute to the value. For example, a 32-bit signed integer type with 8 padding bits would have a maximum value of 223-1, or 16777215
, rather than 231-1, or 2147483647
.
This is stated explicitly in the C standard. I haven't found similar wording in the C++ standard, but I think C++ also permits integer types to have padding bits.
But very few compilers take advantage of this permission.
To determine the maximum value of an integer type, use the appropriate *_MAX
macro defined in <limits.h>
, or in C++ use std::numeric_limits<T>::max()
as suggested by cdhowie's comment.
(Incidentally, the pow
function is not the best way to denote the value you're asking about. It's a floating-point function, and its result may be inexact in some cases.)