Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 453 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does "static" mean in C?

#1
I've seen the word `static` used in different places in C code; is this like a static function/class in C# (where the implementation is shared across objects)?
Reply

#2
From Wikipedia:

>*In the C programming language, **static** is used with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.*
Reply

#3
If you declare a variable in a function static, its value will not be stored on the function call stack and will still be available when you call the function again.

If you declare a global variable static, its scope will be restricted to within the file in which you declared it. This is slightly safer than a regular global which can be read and modified throughout your entire program.
Reply

#4
In C, static has two meanings, depending on scope of its use. In the global scope, when an object is declared at the file level, it means that that object is only visible within that file.

At any other scope it declares an object that will retain its value between the different times that the particular scope is entered. For example, if an int is delcared within a procedure:

void procedure(void)
{
static int i = 0;

i++;
}

the value of 'i' is initialized to zero on the first call to the procedure, and the value is retained each subsequent time the procedure is called. if 'i' were printed it would output a sequence of 0, 1, 2, 3, ...
Reply

#5
`static` means different things in different contexts.

1. You can declare a static variable in a C function. This variable is only visible in the function however it behaves like a global in that it is only initialized once and it retains its value. In this example, everytime you call `foo()` it will print an increasing number. The static variable is initialized only once.

void foo ()
{
static int i = 0;
printf("%d", i); i++
}

2. Another use of static is when you implement a function or global variable in a .c file but don't want its symbol to be visible outside of the `.obj` generated by the file. e.g.

static void foo() { ... }
Reply

#6
Short answer ... **it depends.**

1. Static defined local variables do not lose their value between function calls. In other words they are global variables, but scoped to the local function they are defined in.

2. Static global variables are not visible outside of the C file they are defined in.

3. Static functions are not visible outside of the C file they are defined in.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through