This is not standard C++. Some compilers have an optional extension to allow this non-standard code to compile.
1 2
int x = 2;
int a[x];
While you are learning the language, it is better to stick to standard C++ which will work on any compiler.
1 2
constint x = 2;
int a[x];
What this means is that the size of the array must be known at compile-time (when your source code is being translated into an executable program) rather than at run-time (when you later run the resulting program).
Any worthwhile compiler will have options you can set to disable any extensions and conform strictly to the standards. Have a look at the documentation for your compiler, and I'm sure you'll find it.
@Chervil:
Sorry, I am a beginner and I don't know how to do this in Codeblocks or DevC++.
Please give me a little more explanation on how to do this in codeblocks?
In Code::blocks, select from the menu: Settings->Compiler
and check the appropriate boxes corresponding to the settings below http://i39.tinypic.com/o6wqif.jpg
In DevC++ (Orwell version), from the menu: Tools->Compiler Options
Check the box marked "Add the following commands when calling the compiler" and enter in the text box: -std=c++11 -Wall -Wextra -pedantic-errors
Oh, I tried again and it worked. It means that the compiler needs to be restarted before it works.
Thank you for you consistent Support.
I have my Private messenger enabled. Please mail me your email id so that I can contact you whenever I need help on something( since your explanation is very clear ).