user2487315
user2487315

Reputation: 57

If we define static variable as global then its scope should be within that file but here I'm getting different result

//FILE1.c  
#include<stdio.h> 
#include<conio.h> 
#include "FILE2.c" 
main() 
{
 printf("%d",i);
getch();
}


//FILE2.c

static int i=5; 

Here I'm getting output 5. Why so? It should be an error, isn't it?

Upvotes: 2

Views: 63

Answers (1)

Yu Hao
Yu Hao

Reputation: 122373

Because you included the source file "FILE2.c"(which is not recommended). The preprocessor simply replace the line #include "FILE2.c" with the contents of "FILE2.c".

So there is a variable i in the file FILE2.c which you have known, but also another variable i in the file FILE1.c. Their scope is within their own file(to be precise, their own translation unit).

You should only include header files in C.

Upvotes: 5

Related Questions