5

How can i check by a preprocessor directive if the type unsigned long long is available in the current build environment?

I tried checking

#if __STDC_VERSION__ >= 199901L
    /* Available */
#else
    /* Not available */
#endif

but compiling with gcc and at least without -std=-compiler argument this leads to "Not avaibalble" (but would work).

Is there a better macro to check so that at least it works with C99 standard compilers and with GCC without -std=C99?

urzeit
  • 2,863
  • 20
  • 36

2 Answers2

3

Although it is not the most elegant of solutions, my first instinct would be to check for the existence of the ULLONG_MAX symbol in the limits.h header.

If it is defined, then the unsigned long long int type is almost certainly available. If it is not defined, then the type is probably not available—or at least, it is not well-supported and may only be available as a non-portable compiler extension.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
2

What are you going to do if it is not available? Is your code supposed to compile and work? If not, drop the checking, use unsigned long long, and the compiler will tell you if it cannot handle it.

And if you want a macro that works on C99 compilers - well, that is pointless, since C99 requires unsigned long long.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Pretty sure he answers your second question in the question: *"compiling with gcc and at least without -std=-compiler argument this leads to "Not avaibalble" (but would work)."* – Cody Gray - on strike May 31 '16 at 09:37
  • 1) Then I have to emulate 64 bit integer arithmetics with some smaller `int`s. The code only needs 64 bits for intermediate values. 2) I'd like to check if `long long` is available. The code should compile in every compiler and *at least* detect the mentioned situations where `long long` is available. It does not necessary need to detect a `long long` available in some strange compiler. – urzeit May 31 '16 at 09:53