Using variables in array size inits...legal ANSI C?

Discussion in 'Mac Programming' started by HiRez, Nov 4, 2008.

  1. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #1
    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.
     
  2. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    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).
     
  3. HiRez thread starter macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #3
    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?
     
  4. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #4

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

    b e n
     
  5. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    Use the -std=c99 flag and all will be fine. I think that is the right one, been awhile since I used it.
     
  6. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #6
    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.
     
  7. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7
    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.
     
  8. HiRez thread starter macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #8
    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. :)
     
  9. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #9
    Careful - the full C99 spec is not fully supported by every compiler yet.
     
  10. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #10
    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.
     

Share This Page