質問

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 ; 
役に立ちましたか?

解決

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].

他のヒント

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

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.

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

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(); 

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top