What is a portable method to find the maximum value of size_t? What is a portable method to find the maximum value of size_t? c c

What is a portable method to find the maximum value of size_t?


A manifest constant (a macro) exists in C99 and it is called SIZE_MAX. There's no such constant in C89/90 though.

However, what you have in your original post is a perfectly portable method of finding the maximum value of size_t. It is guaranteed to work with any unsigned type.


#define MAZ_SZ (~(size_t)0)

or SIZE_MAX


As an alternative to bit-operations suggested in the other answers, you could do this in C++

#include <limits>size_t maxvalue = std::numeric_limits<size_t>::max()