4

Is there a standard macro to check support of variable length arrays in C code? It it enough to check for c99 (__STDC_VERSION__ >= 199901L) in all widely used compilers?

11
  • 4
    Since you've tagged C99, you are guaranteed to have VLAs if your compiler is C99 compliant. Commented Apr 23, 2018 at 19:24
  • 1
    Better don't use them. Commented Apr 23, 2018 at 19:26
  • 1
    The non-compliant MSVC does not need a macro. It doesn't compile. Commented Apr 23, 2018 at 19:32
  • 1
    You would only need a macro in C11 when VLAs became optional. Therefore they are not standard so as @i486 says - don't use them. Commented Apr 23, 2018 at 19:34
  • 1
    @abelenky: In some cases, it may be possible to write code which will work with or without VLA support. For example use either int foo[x]; or int *foo = malloc(x * sizeof (int)); (and later free the storage), depending upon whether VLA suport exists. If VLA support exists, and a programmer is willing to chance a stack overflow, the VLA approach may be faster, but code should be usable (but perhaps not as fast) even without VLA support. Commented Apr 24, 2018 at 15:49

1 Answer 1

8

From the C11 specification §6.10.8.3

The following macro names are conditionally defined by the implementation:
[...]

__STDC_NO_VLA__ The integer constant 1, intended to indicate that the implementation does not support variable length arrays or variably modified types.

So if __STDC_VERSION__ > 201000L you need to check __STDC_NO_VLA__.

Otherwise, if __STDC_VERSION__ >= 199901L VLAs should work, but you'll get a compile time error if the compiler is non-compliant.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It looks like what I was looking for. I'll examine it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.