Home > Backend Development > C++ > body text

Can GCC Declare Array Sizes with Non-Constant Variables?

DDD
Release: 2024-10-31 22:57:29
Original
222 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!