Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

HiRez

macrumors 603
Original poster
Jan 6, 2004
6,265
2,630
Western US
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:
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

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
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

macrumors 603
Original poster
Jan 6, 2004
6,265
2,630
Western US
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

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
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.
 

HiRez

macrumors 603
Original poster
Jan 6, 2004
6,265
2,630
Western US
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. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.