How do I share a global variable between c files?
Asked Answered
S

8

79

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?

Scarabaeoid answered 22/7, 2011 at 16:17 Comment(2)
possible duplicate of How do I share variables between different .c files?Medawar
Possible duplicate of How do I use extern to share variables between source files in C?Professorate
S
102

file 1:

int x = 50;

file 2:

extern int x;

printf("%d", x);
Sammiesammons answered 22/7, 2011 at 16:20 Comment(6)
no, i didn't forget anything. I'm just explaining what you would put in those files.Sammiesammons
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.Sammiesammons
@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.Schrimsher
@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 hereScarabaeoid
Can I do the same with an array?Rochellerochemont
Please note that you don't have to use any includes in this case.Fist
P
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.

Pogey answered 22/7, 2011 at 16:22 Comment(2)
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 *);)Gremlin
@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.Mixed
C
2

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

Concelebrate answered 22/7, 2011 at 16:20 Comment(2)
but how should i include the files?is there no need for it?Scarabaeoid
@fresh_graduate: You need not include the file, linker will figure it out for you.Higley
H
1

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

Higley answered 22/7, 2011 at 16:21 Comment(0)
S
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

Saskatchewan answered 22/7, 2011 at 16:49 Comment(0)
M
1

Use extern keyword in another .c file.

Marlanamarlane answered 8/11, 2015 at 17:13 Comment(0)
I
1

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.
Ikeda answered 26/11, 2015 at 12:48 Comment(1)
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?Dichromatic
N
0

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;

Nitz answered 2/9, 2023 at 4:48 Comment(2)
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.Dichromatic
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.Munshi

© 2022 - 2025 — McMap. All rights reserved.