6

When I run the following code,it works fine for C:

#include<stdio.h>

int main(void)
{

const int x=5;
char arr[x];
printf("%d",sizeof(arr));

}

But not only had I read before that const qualified variables are not real constants (that's why they can't be used in case condition of switch-case),but the following link from IBM corroborates that (IBMLINK) and says:

 const int k = 10;
 int ary[k];     /* allowed in C++, not legal in C */

Why then am I allowed to use a const qualified variable in C as an array size without any error?

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
Jugni
  • 255
  • 5
  • 12
  • 2
    Your IBM link is out of date with the C standard. Use a C reference that has been updated during the past 14 years instead. – Lundin May 16 '13 at 14:10
  • 1
    I changed title and text from "index" to "size". The term index is only used when you use an array to indicate one particular member. In a declaration or definition you declare the size of the object as a whole. – Jens Gustedt May 16 '13 at 15:07

1 Answers1

11

c99 support variable length arrays but c90 does not support variable length arrays, you can see this more clearly if you are using gcc and try to compile with these arguments:

gcc -std=c89 -pedantic

this will give you the following warning:

warning: ISO C90 forbids variable length array ‘y’ [-Wvla]

but if you compile using c99 it is perfectly fine:

gcc -std=c99 -pedantic 

As pointed out by John Bode as of the 2011 C standard variable length arrays(VLA) are now optional. Here is a Dr Dobbs article on VLA and also a link to the gcc docs as pointed out by Wayne Conrad.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Does it mean under `c99` the array index can be a return of a function too? – Jugni May 16 '13 at 13:13
  • @Jugni This should be fine – Shafik Yaghmour May 16 '13 at 13:23
  • Shall I conclude that under `c99` there is no rule that that the length of each array must be known to the compiler?That is written in the IBM portal that it must be known to the compiler.So can you confirm last time that there is no such requirement after `c99`?Return of functions, or even general **variables** (with or without `const` qualifier) can be array index? – Jugni May 16 '13 at 13:36
  • @Jugni This Dr. Dobbs article goes into some detail on this topic you might find it useful http://www.drdobbs.com/the-new-cwhy-variable-length-arrays/184401444 – Shafik Yaghmour May 16 '13 at 13:39
  • I'll surely read that,but please answer what I asked in my previous comment.My whole notion about array sizes has been shaken up due to the new revelation.I had assumed it as a strict rule that sizes of static arrays must be known to the compiler. – Jugni May 16 '13 at 13:43
  • @Jugni, Variable length arrays (that is, arrays the length of which are only known at runtime) are a C99 feature. Here's a more succinct reference: http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html – Wayne Conrad May 16 '13 at 13:50
  • @Jugni: note that as of the 2011 C standard, variable-length array support is now *optional* (my understanding is that they are a pain to implement), so some compilers may still give that same error. Also note that VLAs have some restrictions over regular arrays: they cannot be declared `static`, nor can they be used outside of block or function prototype scope. – John Bode May 16 '13 at 14:03
  • @JohnBode Thanks for the details.I would be careful in future not to be taken away by the **sleek** nature of a site,and the big-shot organization behind it (**IBM** for eg). – Jugni May 16 '13 at 14:15
  • @JohnBode Thank you for the note on C11, I added that to my answer. – Shafik Yaghmour May 16 '13 at 14:59
  • 1
    @WayneConrad Thank you for the `gcc` doc link, I added the reference to my answer. – Shafik Yaghmour May 16 '13 at 15:00