2

So, I was teaching base C programming to a student for a test.

Talking about array declaration, I told him:

"you can do this"

int myArray[10];

-> show him that the code compiles

"you can do this, too"

#define ARRAY_SIZE 10
[...]
int myArray[ARRAY_SIZE];

-> show him that the code compiles

"but you can't do this!"

int arraySize = 10;
int myArray[arraySize];

-> show him that the code won't compile...... but it actually compiles!

myWholeLifeIsALie.jpg

I was using DevC++ with MinGW.

Sweating, I switched on Linux and made a simple test program

#include <stdio.h>

int main()
{
  int size;
  int i;

  scanf("%d", &size);

  int array[size];

  for(i = 0; i < size; i++)
    array[i] = i*2;

  return 0;
}

It compiles and run both with g++ and gcc.

Instead, MS Visual Studio 2010 compiler tells me that he "expected constant expression". That's what I was expecting from g++/gcc, too.

I think I'm missing something dumb here, but I can't even...

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
il_mix
  • 553
  • 8
  • 20
  • 4
    Possible duplicate of [Does “int size = 10;” yield a constant expression?](http://stackoverflow.com/q/21273829/1708801) ... `gcc` and `clang` support VLA as a extension in C++ but Visual Studio does not. – Shafik Yaghmour Jan 26 '15 at 13:28
  • 4
    C and C++ are different languages. – Yu Hao Jan 26 '15 at 13:28
  • 5
    As @YuHao points out your question indicates you are using a C++ compiler but you also tagged with C which does not make much sense. Especially in this case since C and C++ are different on this. – Shafik Yaghmour Jan 26 '15 at 13:31
  • A quick web search ("gcc variable size array") would have located [this](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html) and calmed you down a bit. – molbdnilo Jan 26 '15 at 13:33
  • 1
    I tagged with both C and C++ since I've tested it with C (gcc) and C++ (g++). I usually work in C++, so I didn't know about the C "feature". By the way, g++ seems to work like a charm. – il_mix Jan 26 '15 at 13:34
  • 3
    In addition to everything said, whenever teaching a programming language you must make it absolutely sure that you (and the students) are compiling according to the C standard. For gcc you do this with `gcc -std=c11 -pedantic-errors -Wall`, where -std=c11 can be replaced with the standard you are teaching. And if you don't know the differences between C90, C99 and C11, then perhaps you shouldn't be teaching C programming... – Lundin Jan 26 '15 at 13:40

2 Answers2

2

Variable length arrays (length determined by runtime value of a variable) are not supported in standard C++.

However, they are supported in standard C since 1999. Some C compilers predating 1999 and some C++ compilers support VLAs (or a similar feature) as an extension.

Some compiler products/suites (e.g. gcc) have options for selecting support of particular C or C++ standards, and there are options for how picky they are (e.g. what constructs they issue diagnostics or warnings for).

It is necessary to read your compiler documentation, to work out what standards or vendor-specific language features it supports by default, how to change those, and how it issues diagnostics.

Rob
  • 1,966
  • 9
  • 13
0

No sir, your WholeLifeIsNotALie.jpg is also there. :-)

Yes, this is possible in C. Given you compiler supports C99, it allows something called VLA [variable length array].

I'm not much knowledgeable in c++, but if it supports officially, it should be there somewhere defined in the latest standards, otherwise the support comes as a compiler extension.

SideNote: You should always check the return value of scanf() to ensure proper input. Otherwise, you may run into strange things.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Despite that C11 "allows" VLAs, it was marked as optional feature, so conforming implementation may not support it. Anyway it's 100% true for C99. – Grzegorz Szpetkowski Jan 26 '15 at 13:30
  • g++ compiles it, too. Doesn't g++ call cc1plus and all C++ related thing automatically? In other words, doesn't it treat everything in the C++ way? – il_mix Jan 26 '15 at 13:32
  • 1
    g++ supports this as extension. It is not standard C++. – Baum mit Augen Jan 26 '15 at 13:33
  • And it supports it by default, I suppose (since I didn't add any command line parameter). – il_mix Jan 26 '15 at 13:35
  • 2
    @il_mix: The default mode for `g++` is to support GNU extensions (inter alia VLA). You can specify to conform C++ Standard behaviour (which does not include anything like VLAs) by `-std` and `-pedantic` flags. – Grzegorz Szpetkowski Jan 26 '15 at 13:38
  • @GrzegorzSzpetkowski Can you show where it was marked as such? I can point out several references to VLAs in C11. – 2501 Jan 26 '15 at 14:12
  • 1
    @2501: C11 6.10.8.3/p1 (formative) defines `__STDC_NO_VLA__` as `"The integer constant 1, intended to indicate that the implementation does not support variable length arrays or variably modified types."` – Grzegorz Szpetkowski Jan 26 '15 at 14:15
  • @GrzegorzSzpetkowski Thank you, I have just found that as well. But if a compiler supports c99 then making it not support vla in c11 is just weird. – 2501 Jan 26 '15 at 14:17
  • @2501: I believe that intent of Standard Comitee was to lower implementation requirements, so more compilers are able to support C11 with lesser effort. – Grzegorz Szpetkowski Jan 26 '15 at 14:20
  • @GrzegorzSzpetkowski That is reasonable. I'm all for modularity. – 2501 Jan 26 '15 at 14:23
  • and the reason for downvote? – Sourav Ghosh Jan 26 '15 at 14:25