8

I have two C files 1.c and 2.c

2.c

#include<stdio.h>

static int i;

int func1(){
   i = 5;
   printf("\nfile2 : %d\n",i);
   return 0;
}

1.c

#include<stdio.h>

int i;

int main()
{
   func1();
   printf("\nFile1: %d\n",i);
   return 0;
}

I compiled both the files with "gcc 1.c 2.c -o st" The output is as follows

file2 : 5

File2: 0

I was expecting output as follows

file2 : 5

File2: 5

I want to access the same variable "i" in both the files. How can I do it?

Surjya Narayana Padhi
  • 7,741
  • 25
  • 81
  • 130

4 Answers4

15

Choose one file which will store the variable. Do not use static. The whole point of static is to keep the variable private and untouchable by other modules.

In all other files, use the extern keyword to reference the variable:

extern int i;
Mike Weller
  • 45,401
  • 15
  • 131
  • 151
  • I changed the declaration in 1.c to "extern int i;" /tmp/cc6ZXXhV.o: In function `main': 1.c:(.text+0x10): undefined reference to `i' collect2: ld returned 1 exit status – Surjya Narayana Padhi Oct 04 '12 at 13:38
  • 1
    If I remove static would it will be a global variable. It won't be static anymore. – Surjya Narayana Padhi Oct 04 '12 at 13:43
  • 2
    static variables cannot be referenced from outside their compilation unit. You need a global variable. – Mike Weller Oct 04 '12 at 13:44
  • 1
    The only way to do what you want _is_ a global variable. That is the point of _global_ : It's the same for every module. – Gunther Piez Oct 04 '12 at 13:45
  • Ok. So now I got it. The static and global variable works same if we want to use it across files? Am I understanding correct? – Surjya Narayana Padhi Oct 04 '12 at 13:45
  • 1
    @SurjyaNarayanaPadhi: No static and global are orthogonal. You just can't use a `static` variable across files. – Gunther Piez Oct 04 '12 at 13:46
  • 1
    _static_ means it is local to the file, you won't be able to access it anywhere else except in your file. _extern_ means the variable is stored in another file, it is a _global_ variable – Zoneur Oct 04 '12 at 13:47
  • 2
    A static variable cannot be used across files. Note that 'static' when applied to C variables means something entirely different from C++ or Java static member variables. – Mike Weller Oct 04 '12 at 13:47
  • 1
    If I use a static variable across files, its different only in compilation time. After I finish compilation it will be single executable. What will be the difference If I have used it as static or global? – Surjya Narayana Padhi Oct 04 '12 at 13:49
  • @SurjyaNarayanaPadhi If you try to use a static variable across files, you will end up having different variables, one for each file, which have all the same name. – Gunther Piez Oct 04 '12 at 14:10
  • Just curious how does C++ handles this – bharath Apr 24 '17 at 11:55
6

There is never a reason to access a static variable in another file. You don't seem to know why you are using the static keyword. There are two ways to declare variables at file scope (outside functions).

Global variable

int i;

Advantages:

  • is valid throughout the whole program execution.

Disadvantages:

  • can be accessed with extern to create spaghetti code.
  • "Pollutes" the global namespace.
  • Not thread-safe.
  • Initialized at program startup, which creates program overhead.

Local/private variable

static int i;

Advantages:

  • is valid throughout the whole program execution.
  • Can only be accessed by files in the same "module"/"translation unit" (same .c file).
  • Provides private encapsulation since it cannot be accessed by the caller.

Disadvantages:

  • Not thread-safe.
  • Initialized at program startup, which creates program overhead.

My personal opinion is that there is never a reason to use global variables nor the extern keyword. I have been programming for 15+ years and never needed to use either. I have programmed everything from realtime embedded systems to Windows GUI fluff apps and I have never needed to use global variables in any form of application. Furthermore, they are banned in pretty much every single known C coding standard.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • > "There is never a reason to access a static variable in another file" There is: to split a module across different files. – Josu Goñi Apr 06 '20 at 15:42
1

static variables can only be accessed within a single translation unit, which means that only code in the file that it is defined in can see it. The Wikipedia Article has a nice explanation. In this situation, to share the variable across multiple files you would use extern.

Michael Younkin
  • 788
  • 6
  • 11
1

Its not recommended, but we can achieve it using pointers with extern storage class.

In 2.c

static int i;
int *globalvar = &i;

In 1.c

extern int *globalvar;
Donald Duck
  • 8,409
  • 22
  • 75
  • 99