Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it illegal for variables to start with numbers?

Why is it illegal for variables to start with numbers?I know it's a convention but what's the reason?

Edit: I mean variables like "1foo" or "23bar" not only numbers like "3"

like image 639
Nick.h Avatar asked Nov 13 '10 10:11

Nick.h


2 Answers

Because the lexer in most languages will assume you are trying to specify a numeric literal. And then you could declare variables that are indistinguishable from numeric literals, creating a huge bombshell of ambiguity.

like image 55
cdhowie Avatar answered Oct 25 '22 00:10

cdhowie


Pop quiz: in a hypothetical language that permits a variable to begin with a number, what is this?

0xDEADBEEF

In C (and related languages) this can only be a hexadecimal number. If a language allows a variable name to begin with a digit, that could be a variable or a hexadecimal number. That's one quick example of potentially millions.

like image 45
JUST MY correct OPINION Avatar answered Oct 25 '22 01:10

JUST MY correct OPINION