1

Please tell me why the following code runs even on a strict C-99 compiler:

#include <stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    a[1]=10;
    a[2]=5;
    printf("%d %d",a[1],a[2]);
}

The variable declaration must occur before any other statements in C right? If we so want a dynamically allocated array, we have to use memory allocation functions like malloc() but how come it is taking and input integer and allocating that sized array?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
sasidhar
  • 7,523
  • 15
  • 49
  • 75
  • Both [variable-length arrays](http://en.wikipedia.org/wiki/Variable-length_array) and intermingled declarations/code were added in C99. – Mysticial Jul 17 '12 at 05:15

4 Answers4

5

This is known as a variable-length array, and is supported by the C99 standard.
This does not work in C89 or any version of C++.

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
  • this is working on normal C compiler too, but i thought the original C standard doesn't allow it – sasidhar Jul 17 '12 at 05:15
  • C99 also adds the ability to mix declarations and statements within a block. C++ has always had this; C90 did not. – Keith Thompson Jul 17 '12 at 05:15
  • 1
    @sasidhar: Define "normal C compiler", please. Most C compilers don't fully support *any* language standard by default; you typically have to add additional command-line arguments. For gcc, `-ansi -pedantic` causes it to conform quite closely to C89/C90; for (almost) C99 conformance, use `-std=c99 -pedantic`. – Keith Thompson Jul 17 '12 at 05:16
  • 1
    Right! C89/C90 requires all variable declarations to be at the beginning of a block. This is very definitely only valid C99 code. – Prashant Kumar Jul 17 '12 at 05:16
  • 1
    @Prashant: Or it could be taking advantage of a compiler-specific extension; gcc supports mixed declarations and statements even when not in C99 mode. – Keith Thompson Jul 17 '12 at 05:18
  • @Prashant So keeping aside weather it is recommended or not, i can allocate my array anywhere in the code like `C++ or java` for C too? – sasidhar Jul 17 '12 at 05:22
  • Keep the scope of this question focused to C. This code won't work at all on C++. – Prashant Kumar Jul 17 '12 at 05:25
  • I am just asking is it `like` C++ or java now in C (after the introduction of this new rule) – sasidhar Jul 17 '12 at 05:35
  • 2
    If "it" means using non-constant expressions to allocate variable-length arrays, then no. If "it" means intermixing declarations and statements, then yes. – Prashant Kumar Jul 17 '12 at 05:37
  • Works for me in C++ http://cpp.sh/43oh6 Did something change? – gman Jun 28 '20 at 14:13
3

In fact, there are two mechanisms in this code snippet that are not allowed in C90, but are in C99. The first is the variable size array declaration, using a[n]. The second is the mixing of declaration and code, with a being declared after a line of code.

I find this list by David Tribble handy. It is focused around C++ but gives you a good overview about the differences between C90 and C99 as well.

If we so want a dynamically allocated array, we have to use memory allocation functions like malloc()

Apparently you already know, but worth mentioning anyway: malloc() is used to allocate memory on the heap. The array in your example is allocated on the stack, which is a different mechanism. See this answer for an excellent explanation of the differences.

Community
  • 1
  • 1
Reinier Torenbeek
  • 16,669
  • 7
  • 46
  • 69
  • agreed, i am not more concerned about where the allocation is done. My question is weather the C Standard allows it? – sasidhar Jul 17 '12 at 05:17
  • C99 does. See [here](http://david.tribble.com/text/cdiffs.htm#C90-mixed-decl-stmt) for more information about C99 and C90 -- I know that page is mostly about C++ but has good information about C as well, with exact references where to look in the language specifications. – Reinier Torenbeek Jul 17 '12 at 05:22
2

The feature is called variable length arrays, and to answer your question specifically, they were introduced in the C99 standard (probably some C compilers had them before, but any C99-compliant compiler must have them).

You'll find plenty of SO answers recommending you don't use them. With malloc(), there is a defined interface for memory allocation to fail: the call returns NULL. With VLA, there is no such interface: it's undefined behavior what happens when an allocation fails.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
1

C99 allows for variable length arrays as in your example.

here is an artcile about it explaining more

AndersK
  • 35,813
  • 6
  • 60
  • 86