4

In Visual Studio 2005, I'm trying to compile a .c file:

int i = 6;
int a[i];

It doesn't work, so which standard does my compiler follow?

user103214
  • 3,478
  • 6
  • 26
  • 37
  • The workaround is to use `malloc()` -- which has the advantage that it lets you detect allocation failures. – Keith Thompson Oct 24 '11 at 03:24
  • I'm reading a book that follows C89/C99 standard. should i get a new book? also I don't know about gcc. – user103214 Oct 24 '11 at 03:29
  • 1
    Which book is it? The C99 standard is certainly worth knowing, but Microsoft has been particularly slow to adopt it. It's best to be aware of which features are new in C90 and which are available in C90 (which virtually all compilers support). – Keith Thompson Oct 24 '11 at 03:36
  • It is this [book](http://www.amazon.com/Programming-Approach-K-N-King/dp/0393969452) i don't know if book is wrong or compiler is wrong. – user103214 Oct 24 '11 at 03:44
  • 2
    I've heard good things about that book, though I haven't read it myself. Neither the book nor the compiler is actually wrong. The book apparently describes the 1999 version of the C language; the compiler on implements (more or less) the 1990 version. Variable-length arrays were added to the language in 1999. – Keith Thompson Oct 24 '11 at 03:46
  • Possible duplicate of [Enabling VLAs(variable length arrays) in MS Visual C++?](https://stackoverflow.com/questions/5246900/enabling-vlasvariable-length-arrays-in-ms-visual-c) – phuclv Oct 16 '17 at 01:26
  • [Enabling VLAs(variable length arrays) in MS Visual C++?](https://stackoverflow.com/q/5246900/995714) – phuclv Oct 16 '17 at 01:27

1 Answers1

8

Visual Studio only supports C89/90. They have no support for C99. Therefore you cannot use variable-length arrays in Visual Studio. Furthermore, Microsoft has no plans to add support for C99 in their C compiler.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • VS does not even support C89. It supports some bastardization of C that MS is stuck with... – R.. GitHub STOP HELPING ICE Oct 24 '11 at 03:38
  • @R..: Really? From what I've heard, it has pretty decent support for the C89/C90 standard (though you have to give it some non-default options). – Keith Thompson Oct 24 '11 at 03:47
  • 4
    At the very least it fails to correctly support the wide character interfaces added in 1995; for example, `wprintf(L"%s\n", "hello");` does not work. I can't think of the examples right off but I'm pretty sure there are major non-conformance issues with some of the original C89 functions too, and perhaps with the preprocessor's behavior. – R.. GitHub STOP HELPING ICE Oct 24 '11 at 04:11
  • @R.. Uhm, *should* `wprintf(L"%s\n", "hello");` work? I'm sure it should support either `wprintf(L"%hs\n", "hello");` or `wprintf(L"%s\n", L"hello");`... – Medinoc Jun 18 '13 at 15:12
  • 3
    @Medinoc: It should print the string "hello", followed by a newline, to stdout, as required by the C language. Your first alternative is wrong; `%hs` is an invalid format specifier. The second alternative is also wrong. `%s` requires as its argument a pointer to a string (null-terminated array of `char`), not a pointer to a wide string. – R.. GitHub STOP HELPING ICE Jun 18 '13 at 15:20
  • Wow, you're right. The standard does say `%s` always means a narrow string rather than a string "of the same width as the function", and there is no specifier for the latter. Devious and counter-intuitive. – Medinoc Jun 18 '13 at 15:29