: : There is no such thing as "static extern". The whole purpose with
: : static is that the variable can't be accessed outside its own
: : namespace. Also, never ever include .c files or you will get linker
: : errors.
: :
: : Good programming practice would be to write like this:
: :
: :
: : main file:
: : ----------
: : #include <stdio.h>
: : #include "my_file.h"
: :
: : int main()
: : {
: : int i;
: : i = double_me();
: : printf("%d\n", i);
: :
: : getchar();
: : return 0;
: : }
: :
: :
: : myfile.h:
: : ---------
: : #ifndef _MY_FILE_H
: : #define _MY_FILE_H
: :
: : int double_me(void);
: :
: : #endif /* _MY_FILE_H */
: :
: :
: : myfile.c:
: : -------
: :
: : static int i=0;
: :
: : int double_me(void)
: : {
: : i++;
: : return i;
: : }
: :
: : : :
: :
: :
: : This is how all C program should look, more or less. The .h and its
: : corresponding .c file forms an independent code module which may be
: : used in other projects etc.
:
: I 100% agree with you...Actually I am in a impression that if I
: declare a variable as static in global section of my main.c file
: then compiler should not allow it to be defined as extern in any
: other file.
:
: am i right or is there any mistake in this concept?Please clarify.
: my code would be like this...
:
:
: : main file:
: ----------
: #include <stdio.h>
: #include "my_file.h"
:
: static int i;
:
: int main()
: {
: /* int i; */
: i=5;
: i = double_me();
: printf("%d\n", i);
:
: getchar();
: return 0;
: }
:
:
: myfile.h:
: ---------
: #ifndef _MY_FILE_H
: #define _MY_FILE_H
:
: int double_me(void);
:
: #endif /* _MY_FILE_H */
:
:
: myfile.c:
: -------
:
: //static int i =0;
: extern int i;
:
: int double_me(void)
: {
: i++;
: return i;
: }
:
: : :
:
:
it's not possible to access ur static variable in other file