0

I have int a[] in one C file. Now in another .c file I have extern int *a .

I need to know what will be the behavior if I try to access variable a in second file and why? What all problems would occur?

1
  • Sorry that was a typo.actually both have same name 'a' Commented Jun 1, 2013 at 15:20

2 Answers 2

2

Undefined behaviour. So anything might happen.

Sign up to request clarification or add additional context in comments.

5 Comments

could you please elaborate a bit as to why undefined behavior?
@vaibhavshukla: Because the standard says it is. (See section 6.2.7 of C99.) On a more practical level, see some of the answers here: stackoverflow.com/questions/6385850/….
What if I include first file in second? What will be outcome? Linking error or compiler error?
@vaibhavshukla: You could try it... ;)
:) I definitely will. But still want to be shown some more light to the concept before coming to conclusion.
0

If you include the file(declaration) twice or declared variable named a, then there may be error redefinition of a;

when i tried the above compiler shows error :

conflicting types for a;
//previous definition in other file...

1 Comment

@dinesh: I dnt think so bcoz extern keyword says that its a declaration not definition or say when we say extern, it means somewhere in other file variable has been define, so how come redefinition error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.