PDA

View Full Version : Using variables in array size inits...legal ANSI C?




HiRez
Nov 4, 2008, 06:46 PM
Hi, I've been told that it is illegal in ANSI C to use a variable as the size specifier when declaring an array, but I've seen this many times and done it myself with no problems. I can't imagine that's the case, but I couldn't actually find an example in the K&R book where they did that (they do use #defined constants, but not a variable). In Xcode, I create a Standard Tool project, and here is the code:
#include <stdio.h>

int main (int argc, const char * argv[]) {
int n = 0;

printf("Enter number of elements: ");
scanf("%d", &n);

int myArray[n]; // <-- Is this legal ANSI C?

int i = 0;
for (i = 0; i < n; i++) {
myArray[n] = i;
}
return 0;
}
But, I can't get Xcode/GCC to complain about this, no errors and no warnings. I tried setting the "C Language Dialog" to all settings, including "ANSI", "C89", and "C99" (the Xcode default). This is Leopard Xcode 3.1 with GCC 4.0. Can anyone tell me definitively what the deal is? Am I right about this or is there some compiler setting I've missed that is allowing me to do non-strict ANSI C things?

Additionally, I would like to know what Xcode means by "ANSI" in that setting...aren't "C89" and "C99" both ANSI specifications?

Thanks if you can help clear it up.



Guiyon
Nov 4, 2008, 07:11 PM
IIRC, C89/C90/ANSI C are effectively the same standard whereas C99 is a newer standard that adds some modern features to C, including inline functions, single line comments and variable-length arrays. The only problem is that there are few (maybe even none) compilers that actually support the whole C99 Standard. Looking at GCC's C99 status page it looks like variable-length arrays, among many other features, are broken at the moment.

EDIT:
It looks like GCC 'allows' VLA's in C89 mode as well via an extension, even though it's not part of the standard. Also, their page has VLA support listed as broken because it appears like there are some gotchas and it does not strictly follow the C99 standard (there are no details on either of these points).

HiRez
Nov 5, 2008, 11:58 PM
OK, thanks for the info Guiyon. Still wondering why Xcode/GCC is allowing it (without even a warning) if it's not strictly ANSI C. I mean what's the point of specifying ANSI if you're getting something other than the standard, or some variant of the standard?

lazydog
Nov 6, 2008, 05:27 AM
OK, thanks for the info Guiyon. Still wondering why Xcode/GCC is allowing it (without even a warning) if it's not strictly ANSI C. I mean what's the point of specifying ANSI if you're getting something other than the standard, or some variant of the standard?


Have you turned on pedantic warnings messages? I think that will throw up a warning.

b e n

Cromulent
Nov 6, 2008, 09:56 AM
Use the -std=c99 flag and all will be fine. I think that is the right one, been awhile since I used it.

garethlewis2
Nov 6, 2008, 10:52 AM
use the command line switch -pedantic-errors and you will see the error message that used to be spat out by GCC years ago. Variable sized arrays are standard in C++, so it migrated in GCC C which isn't ASNI C or ISO C. Though to be honest, I have never ever seen a fully ISO compliant C or C++ compiler before, they all implement a certain amount of the spec.

Cromulent
Nov 6, 2008, 11:20 AM
so it migrated in GCC C which isn't ASNI C or ISO C.

Variable length arrays are now completely standard C. If you need them, use them. As of C99 that is. It is one of the few safe C99 features across compilers.

HiRez
Nov 6, 2008, 12:31 PM
OK thanks. I know I tried turning on pedantic warnings but I didn't get anything, maybe I didn't get the right detailed switch.

In any case it looks like the answer is that it was a C99 addition that was unavailable in the C89 spec without an extension, that's good enough for me. Since C99 is now the standard on all modern compilers, I'm going to say I was right. :)

Sander
Nov 11, 2008, 03:02 AM
Since C99 is now the standard on all modern compilers, I'm going to say I was right. :)

Careful - the full C99 spec is not fully supported by every compiler yet.

Cromulent
Nov 11, 2008, 03:30 AM
Careful - the full C99 spec is not fully supported by every compiler yet.

In fact the full spec is hardly supported at all. I think the Intel C/C++ compiler is the nearest we get on Mac OS X.