How to make a compilation successful for a program with a variable length array?(currently, Showing error : Variable sized array). I am using gcc in linux. How to make compiler compatible to c99 standard ? PLease help me in this. THanks in advance.

- 14,524
- 7
- 33
- 80

- 21
- 2
-
1With a new enough version of GCC it will automatically use a later version of the C standard. Which version are you using? What are you trying to do? Please [edit] your question to include a [mcve] of your code, together with a full and complete copy-paste (as text) of your errors (with comments added in the code where you get the errors). – Some programmer dude Jul 06 '20 at 04:44
-
Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jul 06 '20 at 04:45
-
3it would have been faster to read the diagnostic message that tells you what to do, than write this question ... – M.M Jul 06 '20 at 04:45
-
@M.M Usually a VLA warning is just to hint that one uses VLAs in their code. It does not tell you what to do to satisfy the compiler. If you don't know what a VLA is nor what its advantages or disadvantages are you got a problem. Asking for that is absolutely appropriate. – RobertS supports Monica Cellio Jul 06 '20 at 07:57
2 Answers
How to make compiler compatible to c99 standard?
By default, the compiler defaults to the most compatible version of C version is installed. Do define the compilation version explicitly, compile the program with the following command-line:
$ gcc -std=c99 -o my_program my_program.c
By defining the -std=c99
, the compiler will be using C99
standard.
Edit: If you're still getting the warning and not the error, then you need to provide your code to know what exactly is wrong.

- 7,482
- 3
- 14
- 34
-
Yes , I tried it . Still no changes in the result. Thank You!!! – Dhana Prakaash Jul 06 '20 at 06:48
-
@DhanaPrakaash Then you need to show your code, we don't exactly know what your code does and the `-std=c99` flag must work correctly. Also, edit the question and add all the information after typing `gcc -v` command in your terminal. – Rohan Bari Jul 06 '20 at 06:50
-
@RohanBari Even with `-std=c99` the *warning* won't go away. The compiler is just hinting. – RobertS supports Monica Cellio Jul 06 '20 at 07:19
-
@RobertSsupportsMonicaCellio the VLAs are supported in C99 standard, ain't it? – Rohan Bari Jul 06 '20 at 07:25
-
@RohanBari https://stackoverflow.com/questions/61825661/why-does-clang-complain-about-using-variable-length-arrays-with-std-c99-flag – RobertS supports Monica Cellio Jul 06 '20 at 07:26
-
@RobertSsupportsMonicaCellio the question is tagged with `gcc` I guess. It's not clang. – Rohan Bari Jul 06 '20 at 07:26
-
@RohanBari It covers in the answers that any compiler can inform you about what it likes to do. I asked for clang, but the answers can be understood for any compiler for the *most* part. – RobertS supports Monica Cellio Jul 06 '20 at 07:29
-
@RohanBari It also explains why at OP's execution the presumed *error* does not go away -> "*Still no changes in the result*". I'm still a kind of confused about the "error" OP seems to get. The use of VLAs shouldn't cause any *error*. – RobertS supports Monica Cellio Jul 06 '20 at 07:40
-
@RobertSsupportsMonicaCellio Yes, that's obviously not an error. I simply answered - *how to make compiler compatible to c99 standard* and that's the possible method to remove the VLA warning. However, I already asked for some code from the OP in order to understand what's actually going on. – Rohan Bari Jul 06 '20 at 07:42
-
@RohanBari I got nothing against your answer. It could have been highly possible that the use of `-std=c99` had removed the warning. I just try to find out what is going on here. Yes, the missing code is unfortunate and disadvantageous. – RobertS supports Monica Cellio Jul 06 '20 at 07:52
-
@RohanBari Also your answer answers perfectly: "*How to make compiler compatible to c99 standard?*" - Maybe you should emphasize this and separate with that both questions. – RobertS supports Monica Cellio Jul 06 '20 at 08:02
"How to make a compilation successful for a program with a variable length array? (currently, Showing error : Variable sized array)."
Usually it isn't an error to compile a code with a VLA unless you compile with -Werror
flag.
The diagnostic you get is high-probably "only" a warning that you use a VLA inside of it, which is risky.
Thus, the compiler informs you about that.
So, you indeed can compile a program with VLAs without any error.
If you got errors they must belong to anything else. We can't find those out since you showed no specific code.
Take a look at this question of mine, not so long ago (even if it is for Clang, it covers the same topic as the answers suggest that a compiler is free to complain about whatever it likes):
Why does clang complain about using variable-length arrays with '-std=c99' flag?
All useful information you can find there.
VLAs are not portable. Try to use alternatives, for example dynamically allocated arrays by using malloc()
.
Related:
"How to make compiler compatible to (the) C99 standard?"
As Rohan in his answer already said, you can use the -std-c99
flag at the invocation of gcc for that. But it probably won't solve your problem to do so.

- 14,524
- 7
- 33
- 80