73

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?

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
peter_perl
  • 733
  • 1
  • 5
  • 5
  • possible duplicate of [How do I share variables between different .c files?](http://stackoverflow.com/questions/1045501/how-do-i-share-variables-between-different-c-files) – 7hi4g0 Jun 03 '15 at 06:16
  • 1
    Possible duplicate of [How do I use extern to share variables between source files in C?](http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c) –  Jun 03 '16 at 05:41

8 Answers8

96

file 1:

int x = 50;

file 2:

extern int x;

printf("%d", x);
Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106
  • 5
    no, i didn't forget anything. I'm just explaining what you would put in those files. – Rocky Pulley Jul 22 '11 at 16:26
  • 3
    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. – Rocky Pulley Jul 22 '11 at 16:27
  • 30
    @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 Jessop Jul 22 '11 at 16:41
  • @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 – peter_perl Jul 22 '11 at 17:22
  • Can I do the same with an array? – Paul G. Apr 16 '21 at 16:21
  • Please note that you don't have to use any includes in this case. – fatiha.kafou May 19 '21 at 14:03
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.

mdm
  • 12,480
  • 5
  • 34
  • 53
  • 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 Jul 22 '11 at 17:08
  • @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. – Michael Burr Jul 22 '11 at 23:29
2

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

Murali VP
  • 6,198
  • 4
  • 28
  • 36
1

Use extern keyword in another .c file.

Kiran Padwal
  • 135
  • 1
  • 4
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.
  • 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? – David C. Rankin Sep 02 '23 at 06:06
1

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

Asha
  • 11,002
  • 6
  • 44
  • 66
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

Matt Sieker
  • 9,349
  • 2
  • 25
  • 43
ami
  • 11
  • 1
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;

  • 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. – David C. Rankin Sep 02 '23 at 06:04