Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check support for variable length array in C

Tags:

arrays

c

c99

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?

like image 283
Seleznev Anton Avatar asked Jan 28 '23 05:01

Seleznev Anton


1 Answers

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.

like image 105
user3386109 Avatar answered Feb 01 '23 12:02

user3386109