-1

I know the keyword in c has two usage:

  • modify variables

    • modify global variables

      this usage limited the scope of global variable range from the point that defined to the end of the file.

    • modify local variables

      this usage limited the scope of local variable in the function that defined, but also remained in the static area of memory

  • modify functions

    this means can only invoke the function in the file it's defined.

and in c++, beside the usage in c, static also used to modify the data member and function member of class. This usage limited the member belong to the class instead of the objects of the class.

I want to know are there anything else usage of static in c/c++?

stamaimer
  • 6,227
  • 5
  • 34
  • 55

3 Answers3

5

static is probably the most confusingly overloaded keyword in both C and C++. It means different things in different places.

  • Within functions, static is a storage class, denoting variables which exist for the lifetime of the programme. So saying

    void f() {
        static int i = 0;
    }
    

    says that the value of i will be preserved between calls to f(). Other storage classes are the default auto (but beware the change in meaning in C++11), extern, and register, plus thread_local in C11/C++11.

  • At file scope (or namespace scope in C++), static is a linkage specifier. Functions and variables marked static in this way have internal linkage, and so are local to the current translation unit. What this means is that a function like

     static int f() {
         return 3;
     }
    

    can only be referenced by other functions inside the same .c file. This usage of static was deprecated in C++03 in favour of unnamed namespaces. I read somewhere it was undeprecated again in C++11.

  • In C++, when applied to a member function or member variable of a class, it means that the function or variable does not need a class instance in order to be accessed. There is little different between "class static" member functions/variables and global functions/variable in terms of implementation, except that C++ class access specifiers apply to members.

  • One last one: in C99 (but not C++), static can be used within an array function parameter, like so:

    void f(int a[static 4]) {
    }
    

    this specifies that the parameter a must by an integer array of size at least 4.

I think that's all of them, but let me know in the comments if there are any I've forgotten!

Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
  • Accidentally made this a community wiki and can't seem to undo it, so please go ahead and update it if you have any more info – Tristan Brindle Dec 20 '13 at 08:21
  • This covers the use of `static` on a data declaration (or at least all of the cases I can think of). What about `static` on a function (where it also has different meanings, depending on whether the function is at namespace or at class scope). – James Kanze Dec 20 '13 at 09:08
2

Static In the C language family, a static variable is one that exists for the lifetime of a compilation unit (a source file or module). A static variable can be declared module-wide, and thus be accessed by all functions defined within the same source file. Such a static variable cannot be directly accessed from other modules, but inner-module API can pass pointers to static variables and modify those through pointers. A static variable can also be declared within a function body, where the usual scope rules apply. A static variable declared within a function is only initialized when the module is initialized (typically when the application loads), and preserves its values over multiple invocations of the function that contains the definition.

In C++, a static variable can also be a member of a class definition. Access to a static member variable is governed by the standard access modifiers (private, public, protected), but all instances of this class share the same static variable, and share the same value. Modifying the value of this variable affects all objects of the class. VolatileThe volatile keyword is something all together different, and not in any way an opposite to static. A static variable may or may not be declared volatile, just as a global or local variable can be. The volatile keyword is a hint informing the compiler that the variable's value might change without the compiler's knowledge. Therefore, the compiler's code optimizer cannot make assumptions about the variable's current value, and must always (re-) read the variable's content.

nuke_infer
  • 63
  • 2
  • 10
  • Note that members can be member functions too, which the second paragraph ignores. Also, besides `volatile`, a static data member can be `const` or `constexpr`. – juanchopanza Dec 20 '13 at 07:38
0

In reference to c++ -> The static keyword can be used to declare variables, functions, class data members and class functions.

Here are the common usages in different scenarios ( ref from MSDN )

  • When you declare a variable or function at file scope (global and/or namespace scope), the static keyword specifies that the variable or function has internal linkage. When you declare a variable, the variable has static duration and the compiler initializes it to 0 unless you specify another value.

  • When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function.

  • When you declare a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. A static data member must be defined at file scope. An integral data member that you declare as const static can have an initializer.

  • When you declare a member function in a class declaration, the static keyword specifies that the function is shared by all instances of the class. A static member function cannot access an instance member because the function does not have an implicit this pointer. To access an instance member, declare the function with a parameter that is an instance pointer or reference.

  • You cannot declare the members of a union as static. However, a globally declared anonymous union must be explicitly declared static.

check following also:
The static keyword and its various uses in C++

static keyword usage

Community
  • 1
  • 1
exexzian
  • 7,782
  • 6
  • 41
  • 52
  • Where do you find that you cannot declare a member of a union as static? The standard seems to allow it. – James Kanze Dec 20 '13 at 09:11
  • @JamesKanze not sure about standards but compiling it here gives error http://ideone.com/hL2PLa – exexzian Dec 20 '13 at 09:16
  • Several points: you should get rid of all of the other errors first (like the missing semicolon), and you should probably add at least one non-static member; I can't find anything saying that an empty union is illegal, but I can't imagine any use for it either. Beyond that: it looks like g++ has a bug here, since it is clearly legal, and other compilers accept it. – James Kanze Dec 20 '13 at 09:43
  • @JamesKanze that still doesn't change the fact that union doesn't accepts static variables. And about other compilers I currently have g++ only can you demo it on other compilers ? – exexzian Dec 20 '13 at 09:46
  • A thought just occurred to me, so I double checked: C++03 does forbid static members of a union. C++11 doesn't. The fact that g++ doesn't accept them (even with -std=c++11) simply means that it hasn't implemented this new feature yet. (Which also explains the text from MSDN which you cite---it was doubtlessly written before C++1, and even if VC++ does accept static members, the text hasn't been updated.) – James Kanze Dec 20 '13 at 09:47
  • The C++ compiler in VS 2012 accepts them. And the C++11 standard is quite clear: "In a union, at most one of the non-static data members can be active at any time[...]" If static data members weren't allowed, they wouldn't have to say "non-static" here (and in fact, C++03 doesn't). – James Kanze Dec 20 '13 at 09:49