+ 14
Why can't a variable name start with a number?
17 Respostas
+ 35
"Allowing use of variable names that began with a number would probably cause huge problems for the language designers. During source code parsing, whenever a compiler/interpreter encountered a token beginning with a digit where a variable name was expected, it would have to search through a huge, complicated set of rules to determine whether the token was really a variable, or an error. The complexity added to the language parser may not justify this feature."
+ 13
I will try to explain this by an example.
You need to define what a Number is. Usually you would say everything containing only digits is a Number. This does not contain floating numbers, but lets forget them for now.
You can do this with a regular Expression:
Number := digit{digit}. {} means it can be repeated 0 to infinite times. 234, 2, 27347748 are all Numbers, but 2Example or asdf234 are not.
Now you want Variables to me meaningfull, but you keep it easy and say Variable := (a-z|A-Z) {a-z|A-Z|0-9} ( "|" stands for OR)
This is easy, asdf234 is a valid Variable, as is counter or Counter or CoUnTEr12.
But look what happens, if you try to have a Variable start with a digit, now:
Variable := (a-z|A-Z|0-9) {a-z|A-Z|0-9},
2Example is a valid Variable, but so is 0983749 or 234524. At the same time a valid Number and a valid Variable. Now you are unable to clearly say which one it is. So you can't parse it and can't translate it accurately.
You can define it in a way to not have this Issue, but maybe this forces much more complexity on your parser/compiler for nearly no pay off.
It depends on different properties of the programming language. This is why there are languages which allow this behavior, but most don't.
In this example saying Variable := {0-9} (a-z|A-Z) {a-z|A-Z|0-9} would make it unique again.
Shortly: C++ has rules which would cause conflicts/ destroy uniques of other rules, if you wanted Variables to start with Numbers, therefore it is forbidden.
This is what Hatsi Rei wrote, clarified by an example.
+ 9
Have you ever heard someone's name begin from number?
Like 1Johny, 36Harry?
Variable names are similar to human names.
As we heard Queen Elizabeth1, Elizabeth2,
the variable names could also be var1, var2 etc.
+ 7
Because the compiler could confuse it with a number literal.
+ 2
@Don yes indeed it is an interesting question. so worth upvoting maybe?
+ 1
There are lots of problems while parsing/compiling code with such variables. So, from the beginning of our days (1970, lol) that's a gold standart, metasyntax. Just the way it is, face it, like you done with fact that you must walk using your feet, not ears or thumbs. Well, at least until moment when you'll write your own compiler or even language.
+ 1
because it will stop lazy programmers to start naming all variables form one to infinite!
0
That's an interesting question...
0
the astric I'd normally a wild card when coding.
0
Because every one is not 50cent
0
telling with basic idea you can say that does any noun contains number? a variable is a name you are giving to allocate a memory...as like as an address
0
h
0
great question
0
If you are programmer you don't have to use number or any alse that not understand, so, make your variable be clear and it will be easer for you.
0
it will be better to use Real name that good to be understand.
0
haha just think about build a string when you combine a variable and numbers like: string result = resultText + 10
if you would write 1resultText the Compiler couldend decide if that is Text or an int. btw never use numbers at all pls not at end and beginning . that can cause difrent problems espacily if you use some older Java compilers.
0
Variables can be one character length, so let’s take the following example:
int 1 = 10;
int x = 1 + 5;
How much is x?
throw new Exception(“I’m so confused!”);