27

As we know, the keyword static has multiple meanings in C. C99 added the possibility of legally writing

void foo (int arr[static 50])
{
    // ...
}

which adds to the confusion, and C++ has static member variables and functions.

This would not be so troublesome if all the uses could be connected in some way, but I find it hard to find that link for some of the cases. Particularly why the static keyword should be used to modify visibility (linkage), or what on earth it's got to do with an array's minimum amount of elements.

So is there a historical reason for the abuse of the static keyword, or is there a secret link under the hood that connects all of its uses?

Community
  • 1
  • 1
  • 1
    What does that syntax in the example code mean? – Kos Jan 06 '11 at 14:31
  • 3
    It means that the "arr" parameter is guaranteed to have at least 50 elements. –  Jan 06 '11 at 14:34
  • 1
    See [here](http://stackoverflow.com/questions/3430315/purpose-of-static-keyword-in-array-parameter-of-function) –  Jan 06 '11 at 14:40

5 Answers5

25

Adding new keywords to a language breaks backwards compatibility. So static gets used where its use might possibly mean something ( int arr[static 50] vs int arr[auto 50] or int arr[extern 50] ) and cannot syntactically appear in that location based its use in previous versions.

Though in that case adding a not_less_than context sensitive keyword in that position would not break previous code, it would add another keyword (so simple text editors which are keyword aware but not syntax aware would not know whether or not it is a keyword), and break the 'keywords are not context sensitive' simplification made in C.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • @Pete: I just can't get this point... What do you mean by "breaks backwards compatibility"? Compatibility with no-more-supported text editors that can't add a new keyword to their list, or do you mean something else? – Roman L Jan 06 '11 at 13:49
  • 14
    Adding `not_less_than` as a reserved word _would_ break previous code if previous code had a variable named `not_less_than`. That's what's meant about breaking backwards compatibility. – Josh Kelley Jan 06 '11 at 13:59
  • 3
    Yes, I think the main reason for "reusing" keywords is that we're certain nobody sane has used it as an identifier or even a `#define` in their code. – aschepler Jan 06 '11 at 14:03
  • @Josh,@aschepler: Oh, if that is what Pete means it makes sense indeed – Roman L Jan 06 '11 at 14:06
  • But what about the original C? Was `static` added so late that anybody had written a substantial amount of code already, possibly using the keyword as an identifier? –  Jan 06 '11 at 14:06
  • 7
    @Øystein: There's a famous quote from (IIRC) Ritchie saying that he'd wanted to change some operator precedences, but they had three installations and hundreds of thousands of lines of code. C solidified before it was designed. – David Thornley Jan 06 '11 at 14:35
  • @David: That's interesting, I never thought it was *that* extreme. –  Jan 06 '11 at 14:38
  • On the other hand, there is this `extern C` already, they could have added something like `extern Cpp` or maybe find a better solution to this, and go on adding new keywords. IMO, backwards compatibility issues should not screw up the language. – Roman L Jan 06 '11 at 14:39
  • 1
    @7vies: There is extern "C++" already, and it's the default in C++. – Fred Nurk Jan 06 '11 at 14:48
  • 4
    @7vies: Sometimes there is no obvious way to add in another construct without another keyword. The C++ Committee used a lot of internet search tools to examine tons of code for use of potential new keywords (so they know that, say, `nullptr` as keyword is unlikely to break anybody's code), but that wasn't really possible last century, when `static` got all those meanings. – David Thornley Jan 06 '11 at 15:11
  • @David: ok, but what stops from disambiguating the language *now*, for example by introducing an alias to static for each of its use, and making the old ambiguous use deprecated? – Roman L Jan 06 '11 at 15:46
  • 1
    @7vies: That is exactly what the C++ committee did with the meaning of file scope: they introduced unnamed namespaces, and deprecated that use of `static` (section D.2). The C committee didn't do that, for reasons I could only speculate on. – David Thornley Jan 06 '11 at 15:54
  • @7vies: And, as Johannes Schaub pointed out in a comment on another answer, the Committee is reversing the deprecation in the latest C++0x draft standard. I don't know why. – David Thornley Jan 06 '11 at 16:21
  • 1
    To add some historical detail. The 1975 C ref Manual available at https://www.bell-labs.com/usr/dmr/www/ describes the use "static" to cause a local variable to persist between function calls, but makes no mention of its use to control visibility. By the time of K&R 1e (1978), this second use of "static" is added. This makes sense. The first use is the more intuitive meaning of "static". The second use is consistent with "we need to solve a different problem [controlling symbol scope], but we can't add a new keyword. – mtk Jun 23 '23 at 10:05
4

There is a very simple way of remembering of all 3 C++ meanings of static I'm aware of. static means "pretty much like global variable/function but only available directly in scope of..."

  • "...this file" if it is in global scope.
  • "...this function" if it is in a function (including member functions). Note that if you make classes and lambdas in a function, they are still in this scope. Lambda with an empty capture can access static variable of its "parent" function.
  • "...this class" if it is in a class (including those declared with struct). This case is slightly different as you can access the variable/function through an object or by prefixing, but this is a little like asking the class or its object to provide you access to it, and it in fact can be denied (with private). So the access isn't "direct".

In case of the presented C99 array syntax, this is something completely different and I assume it was there to not introduce new keywords, as others suggest.

stan423321
  • 183
  • 6
2

static's original meaning in C++ is actually deprecated, replaced with unnamed namespaces. The only way static is actually used in current C++ code is to be non-member.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 5
    Are you forgetting function-level statics? (Which are not deprecated.) I don't know if this is the "original meaning" of static in C, but I believe this use existed before C++. – Fred Nurk Jan 06 '11 at 13:57
  • @Fred: The original meaning of `static` was file-scope variable. Forgot about function-level statics but function-level and class-level statics seem more than similar enough in concept to me to not qualify as a multiple meaning. – Puppy Jan 06 '11 at 14:06
  • @DeadMG: C99's static seems to have quite a different meaning, though – Roman L Jan 06 '11 at 14:17
  • @DeadMG: "The original meaning of `static` was file-scope variable." Are you sure about this? It would make more sense to name the keyword "static" if it specified static storage duration than if it specified internal linkage –  Jan 06 '11 at 14:20
  • @7vies: My answer concerns C++. If you want to ask about C99, open another question. You can't ask the same question about C and C++, because, y'know, they're *different* languages and have *different* answers. – Puppy Jan 06 '11 at 14:21
  • 2
    Actually *I* asked the question :) My question was about the history behind the multiple meanings of "static". This naturally traces back to C, where the keyword originated. I mentioned C99 and C++ because these languages also have multiple meanings for "static". I suspect C# shares some of it too, but I'm not familiar enough with it to say for sure. –  Jan 06 '11 at 14:24
  • @DeadMG: Function-static and static data members are very similar, but I wouldn't group static methods with those. Minor nitpick: static members (both data and function) are still members rather than being non-members. – Fred Nurk Jan 06 '11 at 14:28
  • @Øystein: I believe "static storage duration" was named after the keyword, not the other way 'round. @DeadMG is right (+1) – Billy ONeal Jan 06 '11 at 14:45
  • @Billy: If you say so... –  Jan 06 '11 at 14:48
  • 5
    It's no longer deprecated in C++0x (n3225). – Johannes Schaub - litb Jan 06 '11 at 15:48
1

I think the reasons are different for the different usages that this keyword has. If we take the function scope and file scope use as of classical C for granted (they are at least similar concepts) the first addition off topic is the static in C++ to name a global member of a class.

I guess here the shortcut was just that "static" and "global" seemed to be close enough and early C++ was very careful not to introduce new keywords that would break existing code. So they took an existing one that could not appear in that context.

For the C99 add-on for array parameters things are different, I think, because static is not the only addition, here. You may also have type qualifiers (const and volatile) that qualify the implicit pointer:

void toto1(char str[const 5]);
void toto2(char*const str);

define compatible prototypes. I can only speculate that the choice of the storage class specifier static for the purpose that you mention (minimum length of the array) was seen as a natural extension of that syntax. Also probably it easily proved that this use was compatible with the rest of language, by arguing where a type qualifier may be used to extend the language, a storage class specifier can't do much harm.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
-6

A camel is a horse designed by committee.

http://en.wikipedia.org/wiki/Design_by_committee

ADDED: Committee members involved in the design are conservative, and are more interested in not breaking existing C++ code than the potential elegance of new code.

gtrak
  • 5,598
  • 4
  • 32
  • 41
  • 13
    Could be a nice comment on the question, but makes a very poor answer. – Fred Nurk Jan 06 '11 at 13:17
  • 7
    But C is a horse, and a good one at that. – Matt Joiner Jan 06 '11 at 13:19
  • This is the type of answers you get when you ask a questions like "is there a historical reason for...?" But that doesn't make it a good one. – Cody Gray - on strike Jan 06 '11 at 13:25
  • I suppose it could be interpreted as a way of saying "There are no historical reasons known", which might be the case. I realize odd decisions are made sometimes, but I was hoping someone had a history lesson up their sleeves, or could make a low level connection. –  Jan 06 '11 at 13:32
  • question was 'why?'. It's not clear what kind of answer he wants, but i think what i have now is the specific historical reason, but not the technical answer. – gtrak Jan 06 '11 at 14:26
  • 1
    The question was "why". I agree that there often isn't a good answer to "why" questions, but this isn't a good answer either. It also doesn't necessarily have anything to do with standardization committees: `static` had two distinct meanings (file scope for functions, persistence for variables) before C was standardized. – David Thornley Jan 06 '11 at 15:16
  • Well, if I'm just wrong, that's ok :-). – gtrak Jan 06 '11 at 15:28