Static vs global - c

If I have a C file like below, what is the difference between i and j?
#include <stdio.h>
#include <stdlib.h>
static int i;
int j;
int main ()
{
//Some implementation
}

i has internal linkage so you can't use the name i in other source files (strictly translation units) to refer to the same object.
j has external linkage so you can use j to refer to this object if you declare it extern in another translation unit.

i is not visible outside the module; j is globally accessible.
That is, another module, which is linked to it, can do
extern int j;
and then be able to read and write the value in j. The same other module cannot access i, but could declare its own instance of it, even a global one—which is not visible to the first module.

The difference is that i has internal linkage, and j has external linkage. This means you can access j from other files that you link with, whereas i is only available in the file where it is declared.

i will have static linkage, i.e., the variable is accessible in the current file only.
j should be defined as extern, that is
extern int j;
in another header file (.h), and then it will have external linkage, and can be accessed across files.

Scope of static variable/function is within the same file despite you include the file as part of a different source file.
Scope of global variable is throughout the files in which it is included. To include the variable in a different source file, we use extern before the variable declaration. No memory is allocated again for the variable in this case.
extern is used to declare a C variable without defining it. extern keyword extends the visibility of the C variables and C functions. Since functions are visible through out the program by default, the use of extern is not needed in function declaration/definition. Its use is redundant.

Related

Shall an variable with external linkage be defined with keyword external?

From https://www.quora.com/What-are-the-types-of-linkages-in-C-programming
External linkage, means that the variable could be defined somewhere
else outside the file you are working on, which means you can define
it inside any other translation unit rather your current one (you
will have to use the keyword extern when defining it in the other
source code).
Internal linkage, means that the variable must be defined in your
translation unit scope, which means it should either be defined in any
of the included libraries, or in the same file scope.
None linkage, points to the default functions and braces scopes, such
as defining an auto variable inside a function, this will make the
variable only accessable within that function's scope.
Note that:
Any global object is externally linked by default, you can disable that by using the keyword static.
Any constant global object is internally linked by default, you can
disable that by using the keyword extern.
Suppose a global variable is defined in file1 and I want to use it in file2.
In file1, shall the global variable be defined with keyword
extern?
The quote above seem to contradict itself:
"you will have to use the keyword extern when defining it in the other source code" seems to say it should.
"Any global object is externally linked by default" seems to say that it doesn't need to.
In file2, shall I declare global variable with keyword extern?
tldr: You should declare the variable, with extern, in a header file which is included by both file1 and file2. You should then define the variable in file1 only. To define the variable, you declare it without extern (and also without static).
You sound like you have gotten the extern keyword and the standardese term "external linkage" mixed up.
A variable with "external linkage" is accessible from any function in the program, as long as a declaration of the variable is visible to that function. In contrast, a variable with "internal linkage" is accessible from, at most, one "translation unit" (a single source file and all the files it includes), and a variable with "no linkage" is only visible within a single function. (I don't remember off the top of my head whether a static variable declared within a function is considered to have internal linkage or no linkage.)
The extern keyword, applied to a variable declaration, has two effects. First, it guarantees that that variable will be given external linkage, even if the declaration is inside a function (don't do that). Second, and much more importantly, it makes the declaration not be a definition. What that means is, if you have these two files
/* file1.c */
extern int foo;
/* file2.c */
extern int foo;
int main(void) { return foo; }
their combination is not a valid program. You will get an error from the linker, probably reading something like "undefined reference to foo".
To make this a valid program, you must remove the extern from one of the two declarations of foo. That declaration then becomes a definition, and the program will link. (Also, that declaration can then be given an initializer, if you want it to have a value other than 0 at startup.)
If you remove the extern from both of the definitions of foo, the result is, IIRC, implementation-defined. Some C compilers will merge them into a single global variable, and others will issue a link-time error. Sometimes the behavior depends on whether or not the variables have initializers.
It is OK to write
/* file1.c */
extern int foo;
/* file2.c */
extern int foo;
int foo;
This allows you to put the extern declaration in a header file that both .c files include, which reduces the risk of the declarations coming to have inconsistent types (if this happens, the program will exhibit undefined behavior).
c11 draft, 6.2.2, 4th section, states:
For an identifier declared with the storage-class specifier extern in a scope in which a
prior declaration of that identifier is visible,31) if the prior declaration specifies internal or
external linkage, the linkage of the identifier at the later declaration is the same as the
linkage specified at the prior declaration. If no prior declaration is visible, or if the prior
declaration specifies no linkage, then the identifier has external linkage.
i don't think there will be any difference with or without extern keyword. some experiment also indicates that:
following 2 compiled:
extern int a;
extern int a;
int a = 20;
and
extern int a;
int a;
int a = 20;
but not this one:
extern int a;
int a = 10;
int a = 20;
however, i have some impression that once i read about the different behavior when dynamic linkage get involved in windows. I cannot find it though. it would be nice if anyone can confirm.
The text you quoted has a lot of errors. I would recommend not relying on information from that site.
(you will have to use the keyword extern when defining it in the other source code).
Not true, as file-scope definitions have external linkage unless the static keyword is used (or no specifier is used and they are re-declaring something already declared with the static keyword).
It is possible to include a redundant extern in the definition, however there must also be an initializer (otherwise it would be a declaration and not a definition).
Internal linkage, means that the variable must be defined in your translation unit scope, which means it should either be defined in any of the included libraries, or in the same file scope.
Presumably this means "included headers", not "included libraries".
Any constant global object is internally linked by default, you can disable that by using the keyword extern.
This is wrong, "global objects" have external linkage unless declared with static. The author might be mixing up C with C++ (in the latter, const global objects have internal linkage unless otherwise specified).
Suppose a global variable is defined in file1 and I want to use it in file2.
// file1.h (or any other header)
extern object_t obj;
// file1.c
#include "file1.h"
object_t obj; // or: extern object_t obj = { 1, 2, 3 };
// file2.c
#include "file1.h"

Where can I declare global variable in c program , whether in header or source file [duplicate]

This question already has answers here:
How do I use extern to share variables between source files?
(19 answers)
Closed 6 years ago.
Hi I am a C++ developer now I am doing C programming.
My question is which place is better to declare global variable in c program.
Header or source file (provided my global variable is not used in other files)?
I want that variable like private variable in C++.
Assuming your variable is global and non static.
You need to declare it in a header file. We use extern keyword for this. As pointed out in comments, this keywords is not necessary, but most C users prefer to use it in headers, this is a convention.
stackoverflow.h:
#ifndef STACHOVERFLOW_H
#define STACHOVERFLOW_H
extern int my_var;
#ifndef
And you initialize it in source file. (Use of keyword extern is prohibited if you want to provide an initialization value).
stackoverflow.c
#include "stackoverflow.h"
int my_var = 50;
Do not put initialization value in a header, or you will get a linker error if the header is used at least twice.
Now you can use your variable in any other module by including the header.
main.c
#include <stdio.h>
#include "stackoverflow.h"
int main()
{
printf("my_var = %d\n", my_var);
return 0;
}
Including header "stackoverflow.h" in "stackoverflow.c" is a way to get sure definitions in source file match declarations in header file. This permit to have errors as soon as compilation instead of sometimes cryptic linker errors.
Edit: This is not at all the way to make a variable "private". You have to use a static variable to make it "private". See R Sahu's answer
which place is better to declare a global variable in c program
Answer: In source(*.c) file.
Assume the scenario like, I have declared a variable in a header file. I included this header in two different .c files. After the macro expansion step of compilation, these two files will have the global variable with the same name. So it will throw an error like multiple declarations of the variable during the linking time.
Conclusion:-
Keep all global variable declaration on .c file and put it as static if it is doesn't need in other files.
Add extern declaration of the variable in the corresponding header file if it's needed to access from other files
You should not place global non-constant variables anywhere. Global as in declared with extern and available to your whole project. The need to do this always originates from bad program design, period. This is true for C and C++ both.
The exception is const variables, which are perfectly fine to share across multiple files.
In the case you need file scope variables, they should be declared in the .c file and always as static. Don't confuse these for "globals" because they are local to the translation unit where they are declared. More info about how static file scope variables can make sense.
Also note the C standard future language directions:
Declaring an identifier with internal linkage at file scope without
the static storage class specifier is an obsolescent feature.
So if you don't use static your code might not compile in the next version of the C standard.
If you intend to use the global variables in multiple .c files, it is better to declare them in .h files. However, if you want to keep the variables like private member data of classes in C++, it will be better to provide access to the global data through functions.
Instead of
extern int foo;
Use
int getFoo();
void setFoo(int);
That sort of mimics the private access specifiers for member variables of classes.
Generally what you can do is define the variable in a source file, like int g_foo;, then reference this global in other files with extern, like extern int g_foo; do_sth(g_foo);. You could put the extern int g_foo; declaration in a header file, and include that in other source files. It's not recommend to have definitions of data in header files.
If you want it to be global (external linkage), you should put it in .h file. And this is one of the best practise, I think:
public_header.h
#ifdef YOUR_SOURCE
#define EXTERN
#else
#define EXTERN extern
#endif
EXTERN int global_var;
your_source.c
//your source makes definition for global_var
#define YOUR_SOURCE
#include <public_header.h>
other_source.c
#include <public_header.h> //other sources make declaration for global_var
If you want it to be private (internal linkage), the best solution, I think, is just make definition of it right in your source file instead of header file to prevent the header file is included by another source and then make confuse.
your_souce.c
static int private_var;

Why use the extern keyword in header in C? [duplicate]

This question already has answers here:
How do I use extern to share variables between source files?
(19 answers)
Closed 9 years ago.
Why should I use the extern keyword in the following code:
header.h
float kFloat; // some say I should write 'extern float kFloat;', but why?
file.c
#include <stdio.h>
#include "Header.h"
float kFloat = 11.0f;
main.c
#include <stdio.h>
#include "Header.h"
int main(int argc, const char * argv[])
{
printf("The global var is %.1f\n", kFloat);
return 0;
}
This code works. The global Variable kFloat defaults to external linkage and static lifetime.
Output is:
The global var is 11.0
I don't understand in which case the problem would occur, can anyone give me an example where it would crash?
extern float kFloat;
declares kFloat without defining it.
but:
float kFloat;
also declares kFloat but is a tentative definition of kFloat.
Adding extern just suppresses the tentative definition. In a header file you only want declarations, not definitions.
If the tentative definition is included in several source files, you will end up having multiple definitions of the same object which is undefined behavior in C.
Always put the definition of global variables(like float kFloat;) in the .c file, and put the declarations (like extern float kFloat;) in the header.
Otherwise when multiple .c files include the same header, there will be a multiple definition error.
extern indicates that a variable is defined somewhere in the project (or outside function block) that you want to use. It does not allocate memory for it since you are telling the compiler that this is defined else where.
A variable must be defined once in one of the modules of the program. If there is no definition or more than one, an error is produced, possibly in the linking stage.
Definition refers to the place where the variable is created or assigned storage; declaration refers to places where the nature of the variable is stated but no storage is allocated.
And since it will be accessible elsewhere it needs to be static.
Firstly, it is strange that your code is compiling. It should throw compile time error for double definition of kFloat variable in File.c.
Secondly, if you are trying to use the common variable in two files then it should not be defined in the header.h. You should use extern keyword in header file so that the file which includes the header.h gets to know that it has been defined externally.
Now, you can define the variable globally in any of the c files and then use the variable as a common variable.
Why should I use extern ...?
Well, you shouldn't. Plain and simple. Because you shouldn't use global variables, and they are the only ones that would need the extern keyword.
Whenever you feel tempted to use a global variable, think again. At the absolute maximum, you might need to use a variable with file scope (using the static keyword), typically such a variable would be accompanied by a handful of functions that manipulate/use its value, but the variable itself should not be visible beyond the scope of the file. Usage of global variable only leads to intractably tangled code that is almost impossible to change without introducing a ton of bugs.

Internal linkage with static keyword in C

I know static is an overloaded keyword in C. Here, I'm only interested in its use as a keyword to enforce internal linkage.
If you have a global variable declared in a .c file, what is the difference between using static and not using static? Either way, no other .c file has access to the variable, so the variable is basically "private" to the file, with or without the static keyword.
For example, if I have a file foo.c, and I declare a global variable:
int x = 5;
That variable x is only available to code inside foo.c (unless of course I declare it in some shared header file with the extern keyword). But if I don't declare it in a header file, what would be the difference if I were to type:
static int x = 5.
Either way, it seems x has internal linkage here. So I'm confused as to the purpose of static in this regard.
If you have a global variable declared in a .c file, what is the difference between using static and not using static? Either way, no other .c file has access to the variable [...]
A different file could declare x:
extern int x;
That would allow code referencing x to compile, and the linker would then happily link those references to any x it finds.
static prevents this by preventing x from being visible outside of its translation unit.
There is only one "namespace", so to speak, in C. Without the "static" keyword you are not protected from another file using the name "x" (even if you do not make it visible in your own library's header).
Try to link together several C files containing a non-static variable x (interleaving read and write accesses from functions in each file), and compare with the situation where these variables are declared static.

Why won't extern link to a static variable?

Why does extern int n not compile when n is declared (in a different file) static int n, but works when declared int n? (Both of these declarations were at file scope.)
Basically, why is int n in file scope not the same as static int n in the same scope? Is it only in relation to extern? If so, what about extern am I missing?
The whole and entire purpose of static is to declare that a variable is private to the source file that it is declared in. Thus, it is doing precisely its job in preventing a connection from an extern.
Keep in mind that there are four flavors of file-scope variable definition:
int blah = 0; — blah is defined in this file and accessible from other files. Definitions in other files are duplicates and will lead to errors.
extern int blah; — blah must be defined elsewhere and is referenced from this file.
int blah; — This is the moral equivalent of FORTRAN COMMON. You can have any number of these in files, and they are all resolved by the linker to one shared int. (*)
static int blah; (optionally with an initializer) — This is static. It is completely private to this file. It is not visible to externs in other files, and you can have many different files that all declare static TYPE blah;, and they are all different.
For the purists in the audience: 'file' = compilation unit.
Note that static inside functions (not at file scope) are even more tightly scoped: if two functions declare static int bleh = 0; even in the same file, they are unrelated.
(*): for those of you not familiar: in the usual pattern, one compilation unit has to define a global variable, and others can reference it. It 'lives' in that compilation unit. In case (3), above, no file (or all the files) defines it. If two files say int blah = 0;, the linker will complain of multiple definitions. If two files say int blah; the linker cheerfully creates a single global int and causes all the code to refer to it.
In standard C, there are two scopes for variables declared outside of a function. A static variable is only visible inside the compilation unit (i.e., file) that declared it, and non-static variables are visible across the whole program. An extern declaration says that the variable's location isn't known yet, but will be sorted out by the linker; it's compatible with non-static variables, but extern static is just crazy talk!
Of course, in practice there are other visibilities these days. In particular, there are now scoping levels between that of a single source file and a whole program; the level of a single shared library is a useful one (settable through mechanisms like GCC function attributes). But that's just a variation on the theme of non-static variables; static keeps the same interpretation it had before.
According to MSDN documentation:
When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).
Static (C++) on MSDN: Archived in January 2015; see also the latest documentation: static § Storage classes (C++) | Microsoft Docs
iv.c:2:1: error: multiple storage classes in declaration specifiers
extern static int i;
^
That is what we get on attempting to extern a static variable. Declaring extern static int i; - is analogous to the declaration float int i;
You can't have float and int appear in the same declaration right? Similarly, you can't have extern and static in the same declaration.

Resources