Home >Backend Development >C++ >Can GCC Declare Array Sizes with Non-Constant Variables?

Can GCC Declare Array Sizes with Non-Constant Variables?

DDD
DDDOriginal
2024-10-31 22:57:29311browse

Can GCC Declare Array Sizes with Non-Constant Variables?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn