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?? :-/

Recommended Answers

All 9 Replies

>> 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 ?

#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.

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?

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.

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)

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.

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.?

The IDE should automatically handle linking for source files in your project.

Oh, I was handling files separately.
Thanks for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.