Declaring Array Size with Non-Constant Variable: A GCC Extension
Despite the general understanding that array size declarations in C require constant integer values, it has been observed that certain code, such as:
<code class="cpp">int ArraySize = 5; int MyArray[ArraySize];</code>
compiles successfully in some environments. This exception arises from a GCC extension.
As per the C guidelines, the array bound should be a constant expression. The explanation from The C Programming Language by Bjarne Stroustrup also reinforces this notion. However, GCC allows the use of non-constant variables for array size declarations as an extension to the standard.
For those prioritizing portability, it is recommended to use the '-pedantic' option to receive a warning for such extensions. Alternatively, '-std=c 98' can be employed to treat it as an error and ensure adherence to the standard.
The above is the detailed content of Can GCC Declare Array Sizes with Non-Constant Variables?. For more information, please follow other related articles on the PHP Chinese website!