in Programming in C
624 views
0 votes
0 votes

suppose i have 2 files

//FILE1.h

int var = 10;

 

//FILE2.c

#include<stdio.h>
#include "FILE1.h"

int main()
{
    
    printf("%d",var);
    
}

so this file2.c gives output “10” so why we use “extern” keyword cause without using it our work that is access the variable from other file is easily done without “extern” so what is exact use of extern keyword.

thank you

in Programming in C
624 views

1 Answer

1 vote
1 vote
Best answer
extern is used to refer to variables from different compilation units – not necessarily a file but typically a file. By including one file in another you are currently having a single compilation unit. Compile the two files separately using “gcc -c ” and link them using “gcc” into a single executable. You can see the use of “extern”.
selected by
by

4 Comments

Thanks @Arjun sir

I got it and confusion is clear

1
1

extern is used to refer to variables from different compilation units – not necessarily a file but typically a file

@Arjun Sir What else we can use instead of file ?

0
0
If you include one file in another they become one compilation unit.
0
0
reshown by

yes sir I got this but why you used not necessarily a file?

0
0

Related questions

2 votes
2 votes
2 answers
4
Rohan Mundhey asked in Programming in C Nov 5, 2016
3,610 views
Rohan Mundhey asked in Programming in C Nov 5, 2016
3.6k views