This question already has answers here:
How do I use extern to share variables between source files?
(19 answers)
Closed 8 years ago.
Global value is not accessible in another file?Mycode is below please help me to fix
flie1.c
#include<stdio.h>
extern int i=9;
int main()
{
printf("i m in main\n");
}
file2.c
printf("%d\n",i);
i am compiling both file at once as cc file1.c file2.c
Change it like this, and it will work:
file1.c
#include <stdio.h>
int i = 9; // define the variable
void print(); // declare print: say there is a function called `print` with no arguments and no return type (because the function is DEFINED in file2.c)
int main() {
printf("im in main");
print();
return 0;
}
file2.c
extern int i; // declare "i": say there is a variable called `i` with type of `int` (because the variable is DEFINED in file1.c)
void print() {
printf("%d\n", i);
}
When you have an extern variable, it should also have its 'original' declaration (without using extern). extern simply says ' this variable is defined elsewhere', so you need to define the variable somewhere.
Simply add:
int i=9;
to file2.c (in the top of the file, the 'globals' area')
and you can change your extern declaration to:
extern int i;
(without the assignment of the value 9) in file1.c
Related
This question already has answers here:
Can local and register variables be declared extern?
(5 answers)
Closed 3 years ago.
Assume the following:
test1.h
extern int a;
void init();
test1.c
#include <stdio.h>
#include "test1.h"
int main()
{
init();
printf("%d\n", a);
return 0;
}
test2.c
#include "test1.h"
void init()
{
int a = 1;
}
Above will give me a linker error saying a isn't defined, but it is defined under init().
However, if I change test2.c like this then it will work:
int a = 1;
void init()
{
//
}
Does extern have to be defined globally only?
The internal or external linkage may have only variables that have a file scope.
In your first example
void init()
{
int a = 1;
}
the variable a has a block scope and hence does not have a linkage.
In the header
extern int a
there is declared a variable with external linkage that has the file scope. But it was not defined. So the linker issues an error because it can not find the variable definition.
You may declare a block scope variable with the extern specifier. But it will not be a variable definition. The declaration will refer to a variable with a linkage declared in a file scope.
For example consider the following demonstrative program
#include <stdio.h>
int x = 10;
void f( void )
{
extern int x;
printf( "x = %d\n", x );
}
int main(void)
{
f();
x = 20;
f();
return 0;
}
Its output is
x = 10
x = 20
That is there is defined the file scope variable x with the external linkage. And within the outer block scope of the function f the declaration of the variable x refers to the variable x in the file scope. It does not define a new object.
This question already has answers here:
How to access global variable when there is a local and global conflict [duplicate]
(3 answers)
Closed 3 years ago.
From the below code how to print global value:
Conditions are that, we must not use extern keyword, should not comment the local initialization & should not shift the printf function.
#include <stdio.h>
int a = 20;
int main()
{
int a = 10;
printf("%d",a);
}
I expect the output of 20.
extern will not help as you have another variable with the same name in most inner scope (in this case scope of function main). So you will see this variable instead if the global one.
You need to rename one of those variables.
int a=20;
int main()
{
int b=10;
printf("%d",a);
}
you may have more inner scopes:
int a=20;
int main()
{
int a=10;
{
int a = 5;
{
int a = 2;
printf("%d",a);
}
}
}
and the result is 2
this is the first code
#include <stdio.h>
void a();
int i;
int main()
{
printf("%d\n",i);
a();
return 0;
}
int i=5;
void a()
{
printf("%d\n",i);
}
output
5
5
the second code is
#include <stdio.h>
void a();
extern int i;
int main()
{
printf("%d\n",i);
a();
return 0;
}
int i=5;
void a()
{
printf("%d\n",i);
}
ouput
5
5
what is the difference between the codes if their outputs are same...
and if they are same then what is the use of extern
extern is something like saying to the Linker that you have defined the variable in some other file( even global variables can be made extern inside other functions in same file) don't throw error now you will find the variable later then link it .
using extern your only declaring the variable not defining it(no memory allocated).
In Your second program for first declartion
extern int i;
No memory is allocated but later when you declare
int i = 5 ;
the memory is allocated for i. while linker searches for i in printf if i is not declared extern it throws a error since its extern linking takes place when it finds the defintion of i.
extern means that you are declaring the variable but not defining it (allocating memory for it). Have a look at this link for a good explanation of extern: http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I dont understand why this wouldn't work. Thanks!
#include<stdio.h>
#include<conio.h>
int main()
{
extern int i;
int i=20;
printf("%d",i);
}
Compilation results in the following error:
main.c: In function 'main':
main.c:6:9: error: declaration of 'i' with no linkage follows extern declaration
main.c:5:16: note: previous declaration of 'i' was here
Note: Code compiled online at CompileOnline
This won't work since you're trying to use i in two very different ways in the same scope.
The name i can't refer to both some extern data that someone else is defining, and a local variable.
If you just want to assign to the external variable, don't re-declare it:
extern int i;
i = 20;
You've already declared i as an int in
extern int i;
And then you go ahead and declare it again with
int i=20;
Try doing this instead
extern int i;
i=20;
You are mistakenly re declaring i
#include <stdio.h>
#include <conio.h>
int main()
{
extern int i;
i=20; //Simply assign the value here. why redeclare with `int`
printf("%d",i);
}
int i;
Here variable i is declared and memory is allocated for it but not initialized.
extern int i;
Whenever extern is used, the variable is just declared and memory will not be allocated for it. In order to access it you have to redeclare same variable externally.
Here extern refers to that, you will be defining the value of that variable(i) outside the program(external source). In your case, you do it inside so it will not work as you expected. It can be either defined outside main program or by external programs.
Try this:
#include<stdio.h>
int main()
{
extern int i; //Declared but memory not allocated
printf("%d",i);
return 0;
}
int i=20; //Allocated memory for i and initialized to 20 outside the prog
Output:
20
Global extern variables can also be initialized directly, where as local extern variables cannot be.
#include<stdio.h>
extern int i=10; //Declared, Memory allocated and defined.
int main()
{
extern int j; //Declared but memory not allocated
printf("%d --> %d",i,j);
return 0;
}
int j=20; //Memory Allocated and value defined externally.
Output:
10 --> 20
You can also refer to this link to know more about it.
you have declared i twice which resulted in redefinition error
extern indicates to the compiler that somewhere outside there exists a variable called i which is of type int.
Here, outside could be in the same program or in some other translation(another .c file) unit.
But you're redeclaring the same i which you already declared.
Also, if you're only executing the following program(without linking other files .c) it wont work:
#include <stdio.h>
#include <conio.h>
int main()
{
extern int i;
i=20;
printf("%d",i);
}
It gives a linker error complaining that i is unresolved because the linker was unable to find the definition for i and no storage was allocated to it as it is not defined outside the main.
I got some problem with the following codes particularly in header.c where i can't access the extern int x variable in header.h... Why? Does extern variable in .h not global? How can i use this on other files?
===header.h===
#ifndef HDR_H
#define HDR_H
extern int x;
void function();
#endif
===header.c===
#include <stdio.h>
#include "header.h"
void function()
{
printf("%d", x); //****undefined reference to x, why?****
}
===sample.c===
int main()
{
int x = 1;
function();
printf("\n%d", x);
return 0;
}
The declaration
extern int x;
tells the compiler that in some source file there will be a global variable named x. However, in the main function you declare a local variable x. Move that declaration outside of main to make it global.
The extern keyword say the variable exists but does not create it. The compiler expects that another module will have a global variable with that name, and the linker will do the right thing to join them up.
You need to change sample.c like this:
/* x is a global exported from sample.c */
int x = 1;
int main()
{
function();
printf("\n%d", x);
return 0;
}
extern declares a variable, but does not define it. It basically tells the compiler there is a definition for x somewhere else. To fix add the following to header.c (or some other .c file but only one .c file):
int x;
Note that in main() the local variable x will hide the global variable x.
Indeed extern int x; means x will be defined in another place/translation unit.
The compiler expects to find a definition of x in the global scope somewherelse.
I would reorganise/modify your code like this and get rid of header.c
===sample.h===
#ifndef SAMPLE_H
#define SAMPLE_H
extern int x;
void function();
#endif
===sample.c===
#include <stdio.h>
#include "sample.h"
int x;
void function()
{
printf("%d", x);
}
int main()
{
x = 1;
function();
printf("\n%d", x);
return 0;
}