Ambiguous variable declaration behavior in c

I have the following code

#include<stdio.h> int main() { int a12345678901234567890123456789012345; int a123456789012345678901234567890123456; int sum; scanf("%d",&a12345678901234567890123456789012345); scanf("%d",&a123456789012345678901234567890123456); sum = a12345678901234567890123456789012345 + a123456789012345678901234567890123456; printf("%d\n",sum); return 0; } 

the problem is that we know that the ANSI standard recognizes variables up to 31 characters ... but, both variables are the same up to 35 characters ... but, nevertheless, the program compiles without errors and warnings and gives the correct result ...
but how? Should I give an error message?

+7
c variables variable-declaration
source share
5 answers

Many compilers are designed to exceed the ANSI specification (for example, when recognizing more than 31 variable character names) as protection for programmers. Although it works in the compiler that you use, you cannot count on the fact that it works only in any C compiler ...

+14
source share

[...] we know that the ANSI standard recognizes variables up to 31 characters [...], should it not give a re-declaration error?

Well, not necessary. Since you mentioned ANSI C, this is the relevant part of the C89 standard:

"Limits of implementation"

The implementation must refer to at least 31 characters of the internal name (macro name or identifier that does not have an external binding) as significant. The corresponding lowercase and lowercase letters are different. An implementation may further limit the relevance of an external name (an identifier that has an external reference) to six characters and may ignore the alphabetical differences for such names. 10 These restrictions on identifiers are all from implementation .

Any identifiers that differ by a significant character are different identifiers. If two identifiers differ in non-essential character, the behavior is undefined .

http://port70.net/~nsz/c/c89/c89-draft.html#3.1.2 (emphasis mine)

It is also explicitly described as a general extension:

Length and number of identifiers

All characters in identifiers (with or without external connection) are significant and random differences are distinguished (3.1.2)

http://port70.net/~nsz/c/c89/c89-draft.html#A.6.5.3

So you just use the selection of the C implementation of your compiler.

+10
source share

The C89 rationale details this:

3.1.2 Identifiers

While the implementation is not required to remember more than the first 31 characters of the identifier to match the name, the programmer is actually forbidden to deliberately create two different identifiers, the same in the first 31 characters. Thus, implementations can store the full identifier; they are not required to trim to 31.

The decision to extend the relevance to 31 characters for internal names was made with little opposition, but the decision to maintain the old six-character limit without taking into account the significance of external names was the most painful. While strong feelings were expressed for creating C `` right '', requiring longer names all over the world, the Committee recognized that the language should last for many years, coexist with other languages ​​and with old collectors and linkers. Instead of undermining support for the Standard, severe restrictions have been maintained.

Compilers, such as GCC , can store the full identifier.

  • The number of significant leading characters in the identifier (C90 6.1.2, C90, C99 and C11 5.2.4.1, C99 and C11 6.4.2).

    For internal names, all characters are significant. For external names, the number of significant characters is determined by the linker; for almost all purposes, all characters are significant.

+3
source share

The corresponding implementation must support at least 31 characters for the external identifier (and your identifiers are internal, where the limit is 63 for C99 and C11).

In fact, the presence of all significant characters is the purpose of the standard, but the committee does not want implementations to be inconsistent without providing it. Limitations on external identifiers come from some linkers that cannot provide more (only 6 characters are required on C89, so the old standard library functions have names no more than 6 characters).

To be precise, the standard does not precisely define these limits; the language in the standard is quite permissive:

C11 (n1570) 5.2.4.1 Translation restrictions

An implementation must be able to translate and execute at least one program containing at least one instance of each of the following restrictions: 18)

  • [...]
  • 63 important leading characters in the internal identifier or macro name (each generic character name or extended source character is considered a single character)
  • 31 significant leading characters in the external identifier (each universal symbol name defining a short identifier 0000FFFF or less is considered 6 characters, each universal symbol name defining a short identifier 00010000 or more is considered 10 characters, and each extended source symbol is considered the same number of characters , as the corresponding universal symbol name, if any) 19)
  • [...]

Footnote 18) clearly expresses the intention:

Implementations should avoid introducing fixed translation restrictions when possible.

Footnote 19) refers to the linguistic directions of the future 6.11.3:

Limiting the significance of an external name to less than 255 characters (taking into account each universal symbol name or extended source symbol as a single symbol) is an obsolete function, a concession to existing implementations.

And to explain permissiveness in the first sentence 5.2.4.1, cf. Justification C99 (5.10)

5.2.4 Environmental restrictions

The C89 agreed that the standard should say something about certain opportunities and limitations, but only how to ensure compliance with these clauses of the contract was the subject of serious discussion.

5.2.4.1 Translation restrictions

The standard requires that an implementation can translate and execute some program that meets each of the specified limits. It was believed that this criterion gives useful freedom to the developer in reaching these limits. Although insufficient implementation could probably develop a program that satisfies this requirement, but still succeeds in being useless, the C89 Committee considered that such ingenuity would probably require more work than something useful. The meaning of both the C89 and C99 committees was that developers should not interpret translation limits as values ​​of hard parameters, but rather as a set of criteria by which implementation will be evaluated.

+3
source share

No limit. Actually there is a limit, it must be small enough to fit in memory, but not otherwise. If there is a built-in limit (I don’t believe that there is), it is so huge that it would be very difficult for you to achieve it. I generated C ++ code with 2 variables with a different last character to ensure that names that are long different from each other. I got to a 64 KB file and thought that was enough.

+2
source share

All Articles