-1

Since in C\C++\Java -

int 2a ; //invalid suffix "a" on integer constant

Is there nothing wrong with digits in the rest of variant name although it's valid syntax ?

Like -

int num1 ;
int num_1st ;
int num_2nd ; 
URL87
  • 10,667
  • 35
  • 107
  • 174
  • `int num_1st; int num_2nd;` is better expressed as `int num[2]` which you can use as `num[0]` and `num[1]`. – Nawaz Dec 20 '12 at 18:02

6 Answers6

8

I've never heard of anybody (e.g., any coding standards/guidelines) that had a problem with digits in an identifier. Nonetheless, too many of them can indicate that a vector or array might be preferable -- even with only two, your num_1st and num_2nd might be better as numbers[2].

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
5

An identifier cannot start with a number. It must start with a letter or an underscore.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • 2
    I think he was asking more along the lines of in practice is it bad to have numbers in variable names. He acknowledged that it can't start with numbers. – Ryan Guthrie Dec 20 '12 at 17:55
1

Variables identifier must start with a letter or an underscore, but the rest of the characters can be letter, underscore or a digit.

You can even decalre a variable: int _ = 0;

Or if you are familiar with regular expression, it can be patterned as: "[a-zA-Z_]\w*?\b"

Where the \w*? part is not a must.

StackHeapCollision
  • 1,743
  • 2
  • 12
  • 19
0

Answer is, no there is nothing wrong with numbers in the rest of the identifier name.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • Sure, you **say** there is nothing wrong with numbers in the rest of the identifier name, but I tried using `a3.5`, `b½`, and `cπ`, and the compiler said I was crazy. – Eric Postpischil Dec 20 '12 at 18:31
  • @EricPostpischil Well, in `a3.5` you are trying to use `5` as member variable name, and since it starts with a number, it is not valid identifier name. And the other two are not valid numbers in Java, as reported by the ultimate authority in the matter, `javac` (just try `float f = ½;` or `float f = π;`) ;-) – hyde Dec 21 '12 at 07:18
0

As long as variable names are meaningful, using digits as part of the name is definitely not a bad thing. Of course, having a large number of similarly named variables with just a number at the end to differentiate them could be a sign of bad design.

The reason for nor allowing identifiers starting with digits, I'm pretty sure, is that it makes it so much easier to write the parser:

c = getchar(); 
ungetc(c);
if (isdigit(c)) 
   token = number(); 
else 
   token = identifier(); 
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

This is the rule off identifier that it must start with an letter or an underscore after that only digits are allowed.

user1672134
  • 35
  • 1
  • 8
  • This is stated incorrectly. *"... after that only digits are allowed"* means (in English) that only the first character can be a letter or underscore. That is plainly incorrect. – Stephen C Nov 12 '16 at 10:29