This question already has answers here:
shared global variables in C
(7 answers)
Closed 9 years ago.
I have a file1.c and file2.c. If I define a variable in file1.c, and assign a value to it, how can I access this variable from file2.c?
How should I declare the variable in file1.c and how to retrieve the value from file2.c?
A.c:
int a;
A.h:
extern int a;
That's how. But don't. It's a bad idea.
Related
This question already has answers here:
Why can I define a variable twice in C?
(1 answer)
C - Globally defined vars in different files displaying external linkage without extern modifier
(1 answer)
Advantage of using extern in a header file
(1 answer)
How do I use extern to share variables between source files?
(19 answers)
Closed 1 year ago.
0.c
extern int num;
int main(){
return num;
}
1.c
int num;
2.c
int num = 5;
Above compiles fine with gcc 0.c 1.c 2.c. Why don't I get a multiple definition error during linking?
Isn't int num; under 1.c a full definition?:
tentative definition becomes a full definition if the end of the translation unit is reached and no definition has appeared with an initializer for the identifier.
Or is this undefined behaviour?
This question already has answers here:
Why can I define a variable twice in C?
(1 answer)
C - Globally defined vars in different files displaying external linkage without extern modifier
(1 answer)
Advantage of using extern in a header file
(1 answer)
How do I use extern to share variables between source files?
(19 answers)
Closed 1 year ago.
0.c
extern int num;
int main(){
return num;
}
1.c
int num;
2.c
int num = 5;
Above compiles fine with gcc 0.c 1.c 2.c. Why don't I get a multiple definition error during linking?
Isn't int num; under 1.c a full definition?:
tentative definition becomes a full definition if the end of the translation unit is reached and no definition has appeared with an initializer for the identifier.
Or is this undefined behaviour?
This question already has answers here:
How can I access a shadowed global variable in C?
(6 answers)
Closed 2 years ago.
#include<stdio.h>
int x = 12;
void foo(int x)
{
printf("%d\n",x);
}
int main()
{
foo(3);
printf("%d\n",x);
return 0;
}
This gives output
3
12
How can i access global int x inside the function foo()?
You can, but the parameter shadows it. Just use another name for parameter variable in foo function.
Yes it works like this as variables of same names are allowed provided that they are in different scopes. "x" is both defined in global scope (global variable) and a local scope (in foo() as a parameter). But they are located in different locations in memory.
This question already has answers here:
Using extern keyword to call functions
(4 answers)
Effects of the extern keyword on C functions
(10 answers)
Should functions be made "extern" in header files?
(5 answers)
Is extern keyword for function necessary at all in C?
(4 answers)
Closed 5 years ago.
Is there any difference between declaring a function (bar) this way:
char *foo(char *pch)
{
extern char *bar(); /* this line here */
...
}
Or this way?
char *foo(char *pch)
{
char *bar(); /* this line here */
...
}
The 2011 C Standard says in 6.2.2/5:
If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern.
So there is no technical difference.
But as already noted in the comments, both are considered bad style. A function declaration does not belong inside another function where it will be used. If you use that pattern and want to change the declaration of the function, you would need to find and modify all the places it's used! A function with external linkage should be declared in a header file. A function with internal linkage (using the static keyword) should be declared somewhere near the beginning of a source file.
This question already has answers here:
About Tentative definition
(2 answers)
Why does redefining a static global variable give a compile-time error when redefining a global variable does not?
(1 answer)
Closed 6 years ago.
I create a simple test.c file:
#include <stdio.h>
int a;
int a = 100;
void printA(void)
{
printf("a is %d\n", a);
}
Compile it to generate object file:
$ gcc -c test.c
$
It is OK! But per my understanding, the variable a should be redefined, is it right?
A variable is local to block it is defined.Once code in block is executed variable goes out of scope.In your case firstly a is defined globally.In second case it is defined inside a function.Thus, to compiler both a is different in both cases not the same.