Member Avatar for ArashVenus

my question is , where & when are static and extern used ?

The extern keyword declares that a variable or function is implemented elsewhere, likely in another translation unit (source file). The static keyword is another beast, and depends upon whether it is declared such in a header or in a source file. If in a source file, then that is a local variable or function. If in a header, then a unique instance of that item will be created in each source file that includes the header. In such a case, if it is a function declaration, then each affected source file will have to implement the function. In such a case, it is better to implement the function in the header as an inline function, unless you want differnt implementations for different sources.

Do note that the above does NOT apply to static member functions or variables of classes. In any case, there is a good discussion of these issues here: http://stackoverflow.com/questions/558122/what-is-a-static-function

When we are talking about global or namespace-scope variables or functions, the static and extern specifiers pertain explicitely to the "linkage" of the entity. When extern is specified, it means that the entity has "external linkage", which means that the entity will become a symbol that is visible at link-time across multiple translation units (i.e., "external"). When static is specified, it means the opposite, that the entity has "internal linkage", which means that it will only be visible within the translation unit in which the entity is defined (not the same as declared). If you are confused with some of the terms I used, you should read my tutorial that explains the compilation and linking process in great detail.

By default, everything is extern, so, there isn't much point in using it, except making things more explicit. For static entities, this is mainly useful to "hide" things that are not relevant beyond a single translation unit. Or, when you want something to have its own separate entity for each translation unit (much less common).

Member Avatar for ArashVenus

@mike so simply by using static in every function we mean that we want this variable to be related to the function only , and by using extern we mean that hey function , you can use this , but other functions also need to use this ,
Am I correct ?

@mike so simply by using static in every function we mean that we want this variable to be related to the function only

No -- variables declared within a function are never visible to other functions, and they are auto destroyed as soon as the function returns to it's caller. Making variables within function static means that the variable will hold it's value from one function call to another, they are not destroyed when the function returns like non-static variables. You can easily prove this to yourself by running the following program

int foo()
{
   static int x = 0;
   cout << x << '\n';
   ++x;
}

int main()
{
   for(int i = 0; i < 10; i++)
      foo();
}

Now, run the same program again but this time remove the static keyword. You will see the value of x is always 0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.