tjysdsg
tjysdsg

Reputation: 756

C: Is <limits.h> cross platform?

I want to write C99 or newer code that is as portable as possible. So I want to make sure that is portable.

Upvotes: 3

Views: 705

Answers (1)

The <limits.h> is specified in the C standard. C11/C17 5.2.4.2.1 tells what macros are available when <limits.h> is included. The table also lists the smallest magnitude of values allowed for an implementation. The actual value of each and every macro in the list is implementation-defined.

Notably, this header should be present in any standard-conforming implementation even if the target is a freestanding environment (i.e. does not have the services of the standard library) - e.g. C11 draft N1570 4.6:

  1. [...] A conforming freestanding implementation shall accept any strictly conforming program in which the use of the features specified in the library clause (clause 7) is confined to the contents of the standard headers <float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, <stdint.h>, and <stdnoreturn.h>. [...]

The macros are in C11/C17 CHAR_BIT, SCHAR_MIN, SCHAR_MAX, UCHAR_MAX, CHAR_MIN, CHAR_MAX, MB_LEN_MAX, SHRT_MIN, SHRT_MAX, USHRT_MAX, INT_MIN, INT_MAX, UINT_MAX, LONG_MIN, LONG_MAX, ULONG_MAX, LLONG_MIN, LLONG_MAX and ULLONG_MAX.

Since C89 had neither long long int nor unsigned long long int the last 3 should not be present in a C89-conforming C implementation.

Upvotes: 2

Related Questions