83

If I define a global variable in a .c file, how can I use the same variable in another .c file?

file1.c:

#include<stdio.h> int i=10; int main() { printf("%d",i); return 0; } 

file2.c:

#include<stdio.h> int main() { //some data regarding i printf("%d",i); return 0; } 

How can the second file file2.c use the value of i from the first file file1.c?

2

8 Answers 8

105

file 1:

int x = 50; 

file 2:

extern int x; printf("%d", x); 
Sign up to request clarification or add additional context in comments.

6 Comments

no, i didn't forget anything. I'm just explaining what you would put in those files.
also, file 1 should NOT be a header file, it should be a .c file. File 2 can be a header file which contains the "extern int x" part though.
@fresh_graduate: Putting the line extern int x; in a header file, and including that file from file2.c, has exactly the same effect as putting the line in file2.c. It's up to you how you organise your code, all you need is that after the preprocessor has finished with includes and everything else it does, file 2 has that line in it.
@steve..i can accept your comment as an answer.Knowing about extern,i can find it anywhere in the internet easily but was not able to understand the inclusion of files as how it is done.your comment above cleared everything thanks.Since i cannot accept a comment as an answer +1 for your comment.Nobody has clearly touched the point you mentioned here
Can I do the same with an array?
|
5

Use the extern keyword to declare the variable in the other .c file. E.g.:

extern int counter; 

means that the actual storage is located in another file. It can be used for both variables and function prototypes.

2 Comments

Just for my education, could I see an example of a function prototype declaration using extern? Is it just as simple as putting extern before the prototype? (e.g., extern void doSomething(int, char *);)
@Platinum Azure: in C a function declaration is extern by default. But it hurts nothing to add the extern explicitly, and it would be done just like in your example.
2

using extern <variable type> <variable name> in a header or another C file.

2 Comments

but how should i include the files?is there no need for it?
@fresh_graduate: You need not include the file, linker will figure it out for you.
2

If you want to use global variable i of file1.c in file2.c, then below are the points to remember:

  1. main function shouldn't be there in file2.c
  2. now global variable i can be shared with file2.c by two ways:
    a) by declaring with extern keyword in file2.c i.e extern int i;
    b) by defining the variable i in a header file and including that header file in file2.c.

1 Comment

For 2. above, if you have two .c files and include the header, how many copies of the variable will there be in your program?
1

In the second .c file use extern keyword with the same variable name.

Comments

1

Do same as you did in file1.c In file2.c:

#include <stdio.h> extern int i; /*This declare that i is an int variable which is defined in some other file*/ int main(void) { /* your code*/ 

If you use int i; in file2.c under main() then i will be treated as local auto variable not the same as defined in file1.c

Comments

1

Use extern keyword in another .c file.

Comments

1

use extern keyword in second while defining variable value of first c file. //in first file double z =50;

//in second file extern double x;

2 Comments

Put ```c (3 back-ticks and 'c') above your block of code and ``` below it to format in a fixed font with c syntax highlight. (or indent all by 4-spaces). For code within a sentence, wrap in a single back-tick.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.