Static scope resolution!
Even after looking at a lot of books, I am not able to understand the problem in the following code:
//1.c
static int y=23;
void abc(int a){
printf("%d\n",a);
}
//2.c
#include<stdio.h>
#include "1.c"
int main(){
abc(12);
printf("\n");
printf("%d\n",y);
return 0;
}
I can understand that the functions abc is accessible to this file, but how is this variable y accessible here?The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled.
from: dennis ritchie
So, how is this static variable in 1.c printed here?? :-/
techie1991
Junior Poster in Training
72 posts since Feb 2010
Reputation Points: 36
Solved Threads: 9
>> So, how is this static variable in 1.c printed here??
What happens when the preprocessor processes your #include "1.c" directive that you have in 2.c ?
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
#include "1.c"
This would put the whole code in that file(1.c) as it is to this file(2.c).
I understand your point, but I am confused with the statement. According to my interpretation, a global variable in a file, if declared static, should not be shown (be accessible) in other files that link to the file having the variable.
I am now more confused!! Can you please just explain the statement.
techie1991
Junior Poster in Training
72 posts since Feb 2010
Reputation Points: 36
Solved Threads: 9
This would put the whole code in that file(1.c) as it is to this file(2.c).
Yes, hence your 2.c will turn into ..
static int y=23;
void abc(int a){
printf("%d\n",a);
}
int main(){
abc(12);
printf("\n");
printf("%d\n",y);
return 0;
}
giving you a file-scope variable y perfectly accessible everywhere in 2.c but not in any other file -- does this make things any clearer?
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled.
The term "file" is somewhat misleading. The correct term is "translation unit", which is the fully preprocessed input to your compiler. Because of headers, multiple files can result in one translation unit.
In your case the two files are lumped together during preprocessing into a single translation unit, and that's how the static scope of y extends to both file.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
OK, so the file inclusion makes the two files lump together, and is then served to the compiler as one "translation unit".
So, is their any other way round too, to access a global variable from a file without including it? (for which static plays its role)
techie1991
Junior Poster in Training
72 posts since Feb 2010
Reputation Points: 36
Solved Threads: 9
If the variable has external linkage, you can simply declare it in whatever file you want, provided you link with the object file of the variable's definition:
/* foo.c */
int my_var = 0; /* extern by default */
/* main.c */
#include <stdio.h>
extern int my_var;
int main(void)
{
printf("%d\n", my_var);
my_var = 12345;
printf("%d\n", my_var);
return 0;
}
This is essentially what headers do: provide declarations for external definitions.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I am using codeblocks on windows machine. So, it would be better if you could tell me the linking way too on IDEs.
On linux, if i am not wrong,
gcc 2.c 1.o
would suffice.?
techie1991
Junior Poster in Training
72 posts since Feb 2010
Reputation Points: 36
Solved Threads: 9
The IDE should automatically handle linking for source files in your project.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Oh, I was handling files separately.
Thanks for all the help.
techie1991
Junior Poster in Training
72 posts since Feb 2010
Reputation Points: 36
Solved Threads: 9