Ethical Hacking Programming, Blogging, Hosting, All Computer Software, PC Software Download, JAVA in hindi, HTML, PHP, C, C++, Free Learning, Software's Download, Technical Videos, Technical Tricks and Tips, How Make Money

The static Storage Class Specifier C programming Class 21

The static Storage Class Specifier


The keyword static is a storage class specifier, but it is perhaps better viewed as a storage class qualifier as it imparts di erent properties depending on whether an object is a local variable, an

external variable, or a function. Local variables keep their local visibility but gain static extent.

They are initialised to zero by default and retain their values between function calls.

int increment(void)

{

static int local_static; /* local scope, static extent, initial value 0 */ return local_static++; /* 1st call will return: 0, 2nd: 1, 3rd: 2, ... */

}

External variables and functions that are qualified as static obtain file scope, which means their visibility is limited to a single source file. Their names are not exported to the linker and are not visible to object modules of other source files.3 This prevents unwanted access by code in other parts of the program and reduces the risk of naming conflicts. For example, the following declarations are unrelated and non-conflicting.

File one.c:

static double myvariable;

static void myfunc(int idx);

File two.c:

static int myvariable; /* no conflict with file one.c */

static int myfunc(int idx); /* no conflict */

The example of getch() and ungetch() in the previous section is one situation where static variables would constitute better design. The two functions would remain extern as they might be called from functions in other files, but the variables buffer and bufidx only require file-scope. Thus, static is to preferred over extern where possible as it permits more modular design. As a rule, and where possible, static functions are preferred over external functions, static variables are preferred over external variables, and local variables are preferred over static variables.

Aside. Many programs today operate using multiple threads of control, meaning that various parts of the program operate concurrently, or over a timeslice so as to appear concurrent. In such programs, excessive caution is required when using fixed or external variable rewards. Temporary reliance on the value of the external variable can be violated because DirecTrains Threads They are out and out. In general, avoid the best of static border variables in multi-threaded programs (unless they are clearly not synchronised).

5.4 Scope Resolution and Name Hiding


It is possible for a program to have two variables of the same name with overlapping scope such that they are potentially visible from the same place. In such situations, one variable will hide the other. C specifies that the variable with more restricted scope will be visible, and the other hidden. In other words, the variable that is “more local” has dominant visibility. This situation is demonstrated in the following program.


   #include <stdio.h>


  int modify(int, int);


  int x=1, y=3, z=5; /* external variables */


   int main(void)


In short, for the local variables, the keyword changes to the static extent, but for external variables and functions, while the scope does not change, this radius changes but does not change the boundary
Share:

No comments:

Post a Comment

Follow On YouTube