1

I read in the C99 standard that stdint.h is part of the C standard library.

Do I read correctly that, if I test for C99 compliance, using:

defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)

that means stdint.h is supposed to be available?

Case in point: can I consider an environment which pretends to be C99 compliant but doesn't provide stdint.h to be at odds with its own compliance statement, hence buggy?

Edit : for the curious ones, the system in question is OpenVMS with HP C Compiler (not gcc, which on openVMS does provide stdint.h). So according to answers and comments received so far, I have to consider this implementation (which pretends to be C99) as buggy. For more details : https://groups.google.com/forum/#!topic/comp.os.vms/Bnh3tIOc7bo%5B101-125%5D

Cyan
  • 13,248
  • 8
  • 43
  • 78
  • This isn't really reliable. Solaris 7 for example provides `inttypes` but not `stdint`. I would scrap any ad hoc defines and just use [`autoconf`](https://www.gnu.org/software/autoconf/autoconf.html) – uh oh somebody needs a pupper May 09 '16 at 03:35
  • 2
    @user6292850: Are you saying that Solaris 7 (released in 1998) claimed C99 compliance by defining `__STDC_VERSION__` to a value at least as large as `199901L`? – rici May 09 '16 at 03:38
  • @rici Considering that [inttypes](http://pubs.opengroup.org/onlinepubs/009695399/basedefs/inttypes.h.html) requires `stdint.h`, by definition... – uh oh somebody needs a pupper May 09 '16 at 03:43
  • 1
    @user6292850: stdint.h was added by C99 (and subsequently by Posix-2001 (Issue 6), which you cite) as a subset of inttypes.h. The fact that in 1998 a C compiler provided inttypes.h really says nothing. Such a compiler could certainly be compliant with C90 and any pre-1997 edition of Posix. To be relevant to the OP, Solaris 7 would need to claim C99 compliance, which seems to me to be unlikely. – rici May 09 '16 at 03:53
  • @user6292850: By what definition? Looking at `/usr/include/inttypes.h` on Solaris 9, it appears that it was based on preliminary information about the upcoming C99 standard. Apparently the `stdint.h` header was a later addition. – Keith Thompson May 09 '16 at 03:54
  • @keithThompson: stdint.h is required by free-standing implementations (of C99 and later). inttypes.h had previously been a Posix extension, and not all of it was deemed necessary for freestanding implementations. – rici May 09 '16 at 03:57
  • Unusually, for C, the `` header is required to include ``. Normally one header cannot include any others (unlike C++). But the facilities in `` are useful for (and required in) free-standing implementations — the facilities in `` (only) are not. – Jonathan Leffler May 09 '16 at 05:30
  • If the compiler vendor claims that their compiler is standard compliant, then yes, it is buggy. If the compiler vendor doesn't claim that their compiler is standard compliant, then the rules of the standard don't apply, and it can define `__STDC_VERSION__` to anything it likes despite not conforming. All you can do in that case is protest, or install a standards-conforming compiler. – Jonathan Leffler May 09 '16 at 05:34

2 Answers2

5

Yes.

Incidentally, undefined symbols expand to 0 in preprocessor expressions, so you could just write:

#if __STDC_VERSION__ >= 199901L

On the other hand, an implementation that doesn't claim to conform to C99 (or C11) might still support <stdint.h> as an extension.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • It depends on compilation flags. Some warning just don't like undefined macros and get triggered (see gcc `-Wundef`). – Cyan May 09 '16 at 03:27
  • @Cyan You would have to explicitly enable it as it isn't enabled by `-Wall -Wextra -pedantic`. Furthermore, the define is only missing if you're compiling in `-ansi` mode. But again, if you're worried about standards compliance when compiling in C99 mode, `autoconf` is the only portable way to test for features. – uh oh somebody needs a pupper May 09 '16 at 03:37
  • 1
    @Cyan: The question didn't specify gcc. Any C99 compliant (or C11 compliant) implementation must provide ``. – Keith Thompson May 09 '16 at 03:50
  • @user6292850: As I mentioned, the question didn't specify gcc. But as of release 5, the default language option changed from `-std=gnu89` to `-std=gnu11`, which supports C11 plus GNU-specific extensions. – Keith Thompson May 09 '16 at 03:51
  • As an example of your last point, Microsoft's compiler started providing `stdint.h` some time ago, but does not define `__STDC_VERSION__` (because there are still aspects of C99 that are not supported). – Michael Burr May 09 '16 at 06:00
2

stdint.h is one of the few headers that any conforming implementation is forced to implement. Even various obscure embedded systems compilers have to do this. See normative text C11 chapter 4/6:

A conforming hosted implementation shall accept any strictly conforming program. 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>.

So you can test for __STDC_VERSION__ >= 199901L indeed and then the header must be available. Note that there is no such requirement for inttypes.h.

Case in point: can I consider an environment which pretends to be C99 compliant but doesn't provide stdint.h to be at odds with its own compliance statement (and hence buggy)?

Yes.

Lundin
  • 195,001
  • 40
  • 254
  • 396