4

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,635
  • 15
  • 34
  • 8
    Not the most elegant of solutions, but my first instinct would be to check for the existence of the `ULLONG_MAX` symbol in the `limits.h` header. – Cody Gray May 31 '16 at 09:04
  • Done. I'm sort of surprised you didn't get any better answers. I guess the quick-and-dirty solution is sometimes the best! – Cody Gray Jul 05 '16 at 10:37

2 Answers2

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
  • 47,695
  • 5
  • 65
  • 91
  • 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 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
1

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
  • 222,280
  • 47
  • 466
  • 543