1
#include <stdio.h>
#include <stdlib.h>
int function(int);

int main()
{
    int a=1;
    extern b;//can be only declared
    printf("a=%d\n",function(a));
    printf("b=%d\n",value());
    return 0;
}
int function(int v)
{
    return v+1;
}

In file1.c :

static int b=20;

int value() //can't be used on a static function
{
    return b;
}

In the code above though I've made variable b as static, it's accessible from other files. How can it be accessed, as the static keyword shouldn't allow the variable to be accessed from other files right? I'm bit confused.

James Z
  • 12,209
  • 10
  • 24
  • 44
Meganathan
  • 25
  • 6
  • 1
    C and C++ are two different languages. Please remove the redundant tags. – D-RAJ Jan 30 '21 at 05:08
  • 1
    I don't see that you are accessing `b` directly from `main` or `function`. `value` has direct access to it, and it is returning the value of `b` when called in the `printf` – jkb Jan 30 '21 at 05:19
  • @jkb : It shouldn't be printed right ?? That's were I'm confused – Meganathan Jan 30 '21 at 05:35
  • `static` just means the variable can't be accessed *directly, by name* from other source files. There is nothing wrong with "accessing" it indirectly, by calling a function which does have direct access to it. It is even possible for such a function to return a pointer to the static variable, and the caller can then use that pointer to access the variable directly (but through the pointer, not via the name `b`). – Nate Eldredge Jan 30 '21 at 05:57
  • It shouldn't be thought of like a security feature, as though the compiler would protect the variable from all possible interference by outside code. It's mostly just a way to reduce clutter in the global namespace. – Nate Eldredge Jan 30 '21 at 05:59
  • @NateEldredge : Thanks Nate. So only public variables can be accessed from other files ?? Can we access a variable inside a function ?? – Meganathan Feb 01 '21 at 03:46

2 Answers2

3

This statement

static int b=20;

means the variable b will only be visible in its translation unit i.e. in file1.c, in which you have defined it.

In the main(), which is supposed to be in a different file, you have only declared b but not used it. Try to use it and you will find that linker will fail because it will not find the definition of b.

int main()
{
    int a=1;
    extern int b;    // note the type int in declaration. You have missed it.
    printf("access b = %d\n", b);   // <==== this won't work
    return 0;
}

Try to compile your program with above changes and you will see that the linker will throw error.

With above mentioned changes in main(), just remove the static keyword from variable b definition in file1.c file and above program will link successfully because then b will be a global variable which has external linkage.

From the value() you are returning the value that variable b hold which has nothing to do with whether b is static or non-static.

The lifetime of a static variable is the entire run of the program. You can access the static variable outside of it's scope/translation unit if you have the address of that static variable. Check this:

File file1.c :

#include <stdio.h>

static int b = 20;

int * retaddr_b (void) {
    return &b;
}

void print_b (void) {
    printf ("b = %d\n", b);
}
 

File main.c:

int * retaddr_b (void);
void print_b (void);

int main(void)
{
    print_b();                 // print static variable b value 
    int * ptr_b = retaddr_b(); // ptr_b pointing to b
    *ptr_b = 99;               // dereference ptr_b and assign different value
    print_b();                 // print static variable b value
    return 0;
}

Output:

b = 20
b = 99
H.S.
  • 11,654
  • 2
  • 15
  • 32
  • Thanks H.S. So only public variables can be accessed from other files ?? Can we access a variable inside a function ?? – Meganathan Feb 01 '21 at 03:42
  • @Meganathan There is no any concept of public variables in `C`. Looks like you are confused between `C` and `C++`. Remember one thing, `C` and `C++` are two different languages. Get in-depth knowledge of storage classes in `C`, familiar yourself with scope, lifetime and external/internal linkage of a variable in `C` and also go through this [post](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files). – H.S. Feb 01 '21 at 06:15
1

Its not really accessed there, dint you see below warning when compiled:

warning: type defaults to ‘int’ in declaration of ‘b’ [-Wimplicit-int]

All you are doing is printing the return value from function value.

if you really wanted to see whether it is accessed or not check as below:

comment your printf("b = %d\n",value()); and use printf("b = %d\n",b);
in its place then compile and check.

IrAM
  • 1,720
  • 5
  • 18
  • Thanks IrAM. So only public variables can be accessed from other files ?? Can we access a variable inside a function ?? – Meganathan Feb 01 '21 at 03:44
  • @Meganathan,Its not clear what you are asking, there is no concept of _public variables_ in `C`(At least that i know of), its better open a new question with some example if you still have some doubts – IrAM Feb 01 '21 at 04:51