Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain internal/external variable names?

Tags:

c

variables

In the C book, it says:

At least the first 31 characters of an internal name are significant. For function names and external variables, the number may be less than 31, because external names may be used by assemblers and loaders over which the language has no control. For external names, the standard guarantees uniqueness only for 6 characters and a single case. Keywords like if, else, int, float, etc., are reserved: you can't use them as variable names. They must be in lower case.

Can someone explain what is "internal name", "external names", "external variables"? It would be better if you can make an example.

like image 592
Hu Bin Avatar asked Oct 19 '12 16:10

Hu Bin


People also ask

What are internal and external variables?

Internal variables typically consist of the variables being manipulated and measured. External variables are factors outside the scope of the experiment, such as a participant becoming sick and unable to attend.

What are internal names and external names in C?

"Internal names" are names of identifiers within a function (effectively local variable names). "External names" would be the names of the other identifiers, including the names of functions and any identifiers declared at global scope or declared with storage class extern.

What are the 31 characters in C?

ANSI standard recognizes a length of 31 characters for a variable name. However, the length should not be normally more than any combination of eight alphabets, digits, and underscores. 4. Uppercase and lowercase are significant.

What is external identifier in C?

External identifiers are the ones that have to be visible outside the current source code file. Typical examples of these would be library routines or functions which have to be called from several different source files.


1 Answers

Stroking my white beard and speaking in a sage and pompous voice, I say:

In the olden days, when FORTRAN and COBOL ruled the computing world, the upstart language C had to fit in to existing tool chains. Those tool chains included link-editors (a/k/a linkers, a/k/a loaders) and assemblers that only handled short 6-character symbol (variable and function) names.

C compilers for those tool chains had to pretend that variable and function names were short when they wrote out object files to be consumed by the link-editors. That was the bad news. The good news was that there are plenty of symbols inside C programs that don't need to show up in the object files.

For example, the names of functions ... e.g. "main" and "sqrt" ... need to show up in the object modules, so code from other object modules could use them. So did the names of "extern" style global variables. Those are the external names.

But all the other names in a C program, for example the names of variables in the scope of functions, the names of members of structs, and so forth, didn't have to make it into the object modules. Those are called "internal names."

So, for example, you could have these C variables within a function

 int myFavoriteItem;  int myFavoriteThing; 

and that would be fine. But you could declare them as external variables, like so:

 extern int myFavoriteItem;  extern int myFavoriteThing; 

Some systems would write these names out to the object files as if they were six letters long (because the object files didn't know what to do with longer names). They would then look to the object file as if they had been declared like this.

 extern int myFavo;  extern int myFavo; 

Those would be duplicate declarations. The C compiler was required to catch this kind of thing and throw an error, rather than write a duplicate declaration to an object file. That was a great help to programmers: duplicate declarations in object files generated really obscure link-editor error messages.

The passage you quoted specifies that compilers have to recognize at least 31 characters of an internal name and 6 of an external name. Modern compilers and toolchains no longer have different name-length limitations.

like image 69
O. Jones Avatar answered Sep 22 '22 02:09

O. Jones