Question

This website says that "Don't give similar names to your variables. For example, the compiler may assume that forgiveme and forgivemenot are the same variable. If so, an ugly situation can occur."

I have never read of this problem in variable naming. When will such an ugly situation be most likely to occur? And what do they mean by saying that the compiler may assume that they are not the same variable?

Was it helpful?

Solution

Names should be unique in their first 31 characters

§5.2.4.1 Translation limits of the C11 standard says

The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:18)

— 31 significant initial characters in an external identifier (each universal character name specifying a short identifier of 0000FFFF or less is considered 6 characters, each 18) Implementations should avoid imposing fixed translation limits whenever possible.

The same limit applies for C99 so your example names should be uniquely identified by any standards-compliant compiler from the last 10+ years.

OTHER TIPS

The C (from 1999) standard says in 5.2.4.1 Translation limits:

The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:
— 31 significant initial characters in an external identifier ...

And then in 6.4.2 Identifiers / 6.4.2.1 General:

As discussed in 5.2.4.1, an implementation may limit the number of significant initial characters in an identifier; ...

Any identifiers that differ in a significant character are different identifiers. If two identifiers differ only in nonsignificant characters, the behavior is undefined.

So, forgiveme and forgivemenot being considered the same if there are less than 10 significant characters supported is a nice undefined behavior.

31 is what used to be 6 in the C standard from 1989 (AKA ANSI C).

There was a time, a very long time ago, when the maximum length of a symbol was eight characters. You could use a symbol longer than eight, but only the first eight were significant.

That time passed somewhere in the '80s.

There was a time, and there were (are) compilers that limit the length of a variable name. If, for example, the compiler in question only allows 8 character variable names, then in fact this would be a problem as both forgiveme and forgivemenot would be the same variable forgivem

However, most modern compilers this is a non-issue. If you are working with an arcane compiler, or a small embedded systems compiler that has issues with variable name length, then this could be a problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top