Why use a signed int for size?

In the C API, most sizes are stored using an int, and not an unsigned int. Is there a case where a size would ever be negative? If not, why is int used?

Not sure if this is specifically why, but the google style guide has a section about this: Google C++ Style Guide. They make this recommendation:

In particular, do not use unsigned types to say a number will never be negative. Instead, use assertions for this.

The problem with unsigned ints is that assert(x >= 0) passes trivially.

1 Like