What do we earn by making global variables as non-global variables by using static keyword? [duplicate] - c

This question already has answers here:
Why declare a variable or function static in C?
(4 answers)
Closed 3 years ago.
I have learned that those global variables that will not be used outside the file should be made non-global variable by appending static keyword like static int a;
I want to know that why we should do this?

Static implies a translation unit level scope only.
However, you don't need to make it non-global + static.
You can very well make it global static to curb the scope to translation unit level.

Related

How to initialize a static non-primitive type in Rust? [duplicate]

This question already has answers here:
How do I declare an instance of one of my Rust structs as static? [duplicate]
(1 answer)
How can you make a safe static singleton in Rust?
(3 answers)
How do I create a global, mutable singleton?
(7 answers)
Closed 4 years ago.
I need to have a static std::string::String . I tried initializing it in several ways but in vain. One of them being -
static mut DURATION_HEX:String = "0".to_string();
I get a note saying :
note: a limited form of compile-time function evaluation is available
on a nightly compiler via const fn
I am not able to initialize it. I cannot leave it without initialization as well.
How do I create a static variable of non-primitive type in rust?

Use of static keyword to global variable in c [duplicate]

This question already has answers here:
What does "static" mean in C?
(20 answers)
Closed 5 years ago.
If I apply static keyword to local variable, its value is maintained within function invocations. However, if I apply static keyword to global variable, will it be as good as declaring the variable as global? What I mean to say is does static keyword has any effect on global variables?
It means that variable has file scope. You cannot access that global variable (by name) from another file.

Are global static variables within a file comparable to a local static variable within a function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I know declaring a global variable as STATIC will make it visible to the current file. Does the variable retain its data every time functions are called within the file?
For example,
Let's say some file calls func1() below, which modifies the static global variable data and then calls func2() which modifies it again.
The next time a file calls func1(), will it be modifying a new data variable struct? or will it preserve the previous data that was modified in the first call?
STATIC MY_DATA Data1;
void func1( ){
//modify Data1
func2(Data1);
}
void func2 (MY_DATA data){
// modify data
}
The keyword static has a different meaning at file scope then it does in a local scope.
At file scope, static declares internal linkage. That means only other code in the same translation unit can see the declared variable (or function); if the same name appears in a different file then it designates a separate variable (function). The opposite is external linkage, which you can declare explicitly via the extern keyword. Additionally, variables declared within a function scope have no linkage -- they can be accessed only within the function wherein they are declared.
In a local scope, on the other hand, static declares static storage duration, as opposed to the default of automatic duration. Variables with static duration are initialized at program startup and retain their values across function calls, unlike variables with automatic duration.
All variables declared at file scope and all functions have static duration, so although it is potentially confusing, there is no essential ambiguity in using the same keyword for these two distinct purposes in different contexts.
As for your example, whether your file-scope variable is declared static or not, it has static storage duration. Its value will therefore persist across function calls, and all functions in the same file will have access to its current value at all times. If it is not declared static then it can be accessed by other functions, too.

Is it possible to have a stand-alone static variable within a function? [duplicate]

This question already has answers here:
Static function variables in Swift
(4 answers)
Closed 8 years ago.
How can I have a static variable within a function as shown below?
func addItem(petItem:Item) -> Bool {
var static count = 0 //... pseudo code
...
...Or must I have all static codes at the top of the file (global area)?
Or must I have all static codes at the top of the file (global area)?
Yep, that. You need to declare your variable in the scope where it will persist. Exactly where that should be depends on what you are trying to achieve.

extern storage class and global variables [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are extern variables in C?
What is the difference between extern storage class and global variables in C programming language.? To me it seems like they both are same thing .Please clarify.
an extern variable is a declaration of a variable which is defined in another unit.
You declare it in .h file:
extern int global_var;
and if you want to use it in .c file you define it in the global scope. And you should include the .h file in the source file that uses it.
Global variables are ariables which were declared outside of a block.
They can be accessed everywhere in the program.
Please note that it is very important to know the difference between Declaring a variable and Defining it:
Declare a variable - There is something with this name, and it has this type. The compiler can use this variable without the need of all the definition of it.
Define a variable - Provide all of the information to create this variable.

Resources