Home > database >  Why does c use abbreviations for everything?
Why does c use abbreviations for everything?

Time:03-21

For example, why does the language use argv instead of argumentVector, or argVector (or argument_vector, depending on your preference) or malloc instead of allocateMemory or allocMem? Is there a justification? It seems to me that the abbreviations selected are also fairly obtuse. This is especially evident in the case of malloc, where "m" is placed before "alloc", which is especially unintuitive. Is there a way to go about thinking about this that will make it clearer and more readily apparent, or is this just a barrier to entry that I'll need to memorize?

Also, I've only been able to find answers about why people who program in c abbreviate extensively. This is about abbreviation in c as a language, not abbreviation as a stylistic convention.

CodePudding user response:

I'm quoting my teacher here. He said that, back in the day, monitors were way smaller so if you use long names, your code won't fit on your screen anymore. C is an old language (the '70s) so it's basically a bunch of legacy we have to deal with now. Changing it would break to much existing code (Linux/unix is written in C for example).

Also the people using it are sometimes from that time so they are used to this style.

CodePudding user response:

Historical note:

70's and early 80's era compilers allowed symbol names that were greater than 8 characters. Meaning that the compiler would parse them. But only the first 8 characters were stored into the symbol table and treated as significant/different when comparing symbol names.

So, you could have longer symbol names but the additional characters were ignored, so automatic variables (function scope) had to be unique in the first 8 characters:

int
main()
{
    int abcdefg0;  // unique
    int abcdefg1;  // unique

    int abcdefgh0;  // not unique
    int abcdefgh1;  // not unique

    int bcdefghiXXX;  // unique but truncated to [treated as] bcdefghi

    return 0;
}

The linker/loader allowed 8 character names. But, for a global (e.g.):

int foo;

int
bar()
{
    return 0;
}

The compiler would change the names by adding a prefix of _ [to distinguish C symbols from asm symbols]. So, we'd get _foo and _bar respectively in the .o file.

So, C global names had to be unique in the first 7 characters. Otherwise, abcdefg0/abcdefg1 (in the source file and compiler symbol table) --> _abcdefg0/_abcdefg1 (with compiler adding the prefix) --> _abcdefg/_abcdefg (in the .o)

(i.e.) A collision when linking.

Because of this, IIRC, most programmers kept even automatic/function scoped variables unique in the first 7 characters (even though they could be unique in the first 8) because it kept things simple when changing a function scope variable into a global scope variable.

At least that's what I did back then.

  •  Tags:  
  • c
  • Related