18

When taken literally, it makes sense, but what exactly does it mean to be a significant character of a variable name?

I'm a beginning learner of C using K&R. Here's a direct quote from the book:

"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 only for 6 characters and a single case."

By the way, what does it mean by "single case"?

J. Mini
  • 1,868
  • 1
  • 9
  • 38
withchemicals
  • 357
  • 3
  • 8
  • If your just beginning C, why are you wanting to know K&R? Ansi C89/99 is much more "sane" and popular. – Earlz Dec 30 '09 at 16:15
  • I wasn't sure of which book to buy in order to learn C. I was told by another person that K&R could teach me how to program in C perfectly, but so far it seems to be a bit hard to understand sometimes. It seems to be a bit vague, full of brevity and advanced jargon. I may be wrong however... – withchemicals Dec 30 '09 at 16:22
  • 3
    “K&R C” was the original, now obsolete, dialect of C introduced by the first edition of the K&R book. I expect (and hope!) you actually have the second edition of K&R, which does cover ANSI C. It's a great book, but there have been further releases of the ANSI C standard since. The changes are not huge, but worth knowing about. You now get some new types like booleans and long-longs, and, yes, the crazy-short identifier limits are greatly raised. – bobince Dec 30 '09 at 16:29
  • Yup! I do have the second edition of K&R that covers ANSI C. Oddly enough, I jumped into this book without fully grasping what ANSI C is. To my understanding, it's simply a standard method of writing code so that anyone who knows the ANSI standard will understand the aforementioned code (hopefully my understanding is correct). This is similar to how many dialects are spoken in China but Mandarin is the official spoken language(?). Just out of curiosity, will code in C still compile if it does not conform to the ANSI standard? – withchemicals Dec 30 '09 at 16:34
  • 2
    The ANSI standard defines what a particular version of the C language is. It's up to each individual compiler what it does with code which does not conform to the standard. Most (all?) compilers support some extensions. If a particular program doesn't conform to the standard, then it's debateable whether it should even be called "C", as opposed to for example "GNU C", or "C with such-and-such extensions". – Steve Jessop Dec 30 '09 at 17:31

5 Answers5

21

Single Case usually means "lower case". Except in some OS's where it means "upper case". The point is that mixed case is not guaranteed to work.

abcdef

ABCDEF

differ only in case. This is not guaranteed to work.

The "Significance" issue is one of how many letters can be the same.

Let's say we only have 6 significant characters.

a_very_long_name

a_very_long_name_thats_too_similar

Look different, but the first 16 characters are the same. Since only 6 are significant, those are the same variable.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 1
    You're awesome! So just making sure, does this quote: "For external names, he standard guarantees only for 6 characters and a single case."" mean that AbCDeF and abcdef and ABCDEF are all not 100% guaranteed to be different variables? Also, as a beginning programmer, should I be worrying about what the "assemblers and loaders" mentioned are? – withchemicals Dec 30 '09 at 16:17
  • 2
    Correct. Things that differ only by case are not GUARANTEED to work. They may work, but that's an accident. You won't be using an assembler any time soon. Your loader is part of your operating system. Most loaders handle very long names. When I was a n00b programmer (in the 80's) the 6-character thing was serious on a few OS's. For standard OS's (Linux, MacOS, etc.) no worries. For Windows, almost no worries. – S.Lott Dec 30 '09 at 16:55
7

It means what you fear it means. For external names, the C standard at the time K&R 2nd ed. was written really does give only six case-insensitive characters! So you can't have afoobar and aFooBaz as independent entities.

This absurd limitation (which was to accommodate legacy linkers now long-gone) is no longer relevant to any environment much. The C99 standard offers 31 case-sensitive characters for external names and 63 internally, and commonly-used linkers in practice support much longer names.

bobince
  • 528,062
  • 107
  • 651
  • 834
3

It just means that if you have two variables named

abcdefghijklmnopqrstuvwxyz78901A,

and

abcdefghijklmnopqrstuvwxyz78901B,

that there is no guarantee that will be treated as different, separate variables...

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
2

It means that :

foobar1
foobar2

might be the same external name, because only the first 6 characters need be considered. The single case means that upper and lower case names need not be distinguished.

Please note that almost all modern linkers will consider much longer names, thogh there will still be a limit, dependent on the linker.

0

G'day,

One of the problems with this limited symbol resolution occurs at link time.

Multiple symbols with the same name can exist across several libraries and the link editor usually only takes the first one it finds that matches what it is looking for.

So, using S.Lott's example from above, if your link editor is searching for the symbol "a_very_long_name" and it finds a library on its search path that contains the symbol "a_very_long_name_thats_too_similar" it will take this one. This will happen even if the library that contains the symbol that you want, i.e. "a_very_long_name" has been specified in your command. For example specifying the libraries as:

-L/my/library/path -lmy_wrong_lib -lmy_correct_lib

There are now compiler options, or more correctly compile time options which are passed through to the link editor, which enforce a search for multiple symbols in your link path. These are then usually raised as errors at link time.

In addition, many compilers, e.g. gcc, will default to such behaviour. You have to explicitly enable multiple definitions to allow the link editor to proceed without raising a fatal error if it finds multiple definitions for a symbol.

BTW I'd highly recommend working through the exercises in conjunction with Clovis Tondo's book "The C Answer Book 2nd ed.".

Doing this really helps make C stick in your mind.

HTH

cheers,

Community
  • 1
  • 1
Rob Wells
  • 36,220
  • 13
  • 81
  • 146