470,849 Members | 1,095 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Regarding extern

HI

I wrote a simple program to expect some error, but it is printing the
value.
main()
{
extern int i;

int i=10;
printf("%d\n",i);
}
I am getting answer 10. But i think it should not be the case.

Thanks

Jun 27 '08 #1
5 945
pr****************@gmail.com said:
HI

I wrote a simple program to expect some error, but it is printing the
value.
main()
{
extern int i;

int i=10;
printf("%d\n",i);
}
I am getting answer 10. But i think it should not be the case.
Given that you've failed to provide a prototype for printf, no output that
printf provides can be considered incorrect. C headers are not eye candy.
Add #include <stdio.hto your code, above main.

What do you think *should* be the case? What were you expecting to be
printed, and why? And what diagnostic messages does your compiler give you
when you invoke it in conforming mode?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #2
On Fri, 11 Apr 2008 23:49:37 -0700, pr****************@gmail.com wrote:
HI

I wrote a simple program to expect some error, but it is printing the
value.
main()
{
extern int i;

int i=10;
printf("%d\n",i);
}
I am getting answer 10. But i think it should not be the case.
The program is indeed broken (also for other reasons than you mentioned).
It is not allowed to declare two different objects with the same name in
the same scope. I have tried various compilers, and almost all (not all)
give a hard error for your code.

That said, compilers _are_ allowed to compile any code, no matter how
broken, so long as they complain that the code is broken. Which compiler
are you using, and have you checked the documentation to see how to
enable all standard errors or warnings?
Thanks
Jun 27 '08 #3
Hi,
Here in the program
main()
{
extern int i; -->Global 'i'
int i=10; --->local 'i'
printf("%d\n",i);-->complier always refers to the nearest block
variable 'i'(i.e., local variable 'i' which is value 10
}
so the o/p is always '10'

On Apr 12, 11:56 am, Harald van Dk <true...@gmail.comwrote:
On Fri, 11 Apr 2008 23:49:37 -0700, prashant.khade1...@gmail.com wrote:
HI
I wrote a simple program to expect some error, but it is printing the
value.
main()
{
extern int i;
int i=10;
printf("%d\n",i);
}
I am getting answer 10. But i think it should not be the case.

The program is indeed broken (also for other reasons than you mentioned).
It is not allowed to declare two different objects with the same name in
the same scope. I have tried various compilers, and almost all (not all)
give a hard error for your code.

That said, compilers _are_ allowed to compile any code, no matter how
broken, so long as they complain that the code is broken. Which compiler
are you using, and have you checked the documentation to see how to
enable all standard errors or warnings?
Thanks
Jun 27 '08 #4
On Sat, 12 Apr 2008 02:24:13 -0700, Pradeep wrote:
Hi,
Here in the program
main()
{
extern int i; -->Global 'i'
int i=10; --->local 'i'
printf("%d\n",i);-->complier always refers to the nearest block variable
'i'(i.e., local variable 'i' which is value 10 }
so the o/p is always '10'
No, the first declaration of i is not a "global" declaration. It is a
block scope declaration of a variable. It will have to be defined
somewhere else with file scope, but that's irrelevant.

Consider this program:

int main(void) {
extern int i;
extern int f(void);

return f();
}

int f(void) {
return i;
}

int i;

This is invalid. Inside of f, the previous declaration of i is not
visible. Even though it refers to an object with file scope, the
declaration itself has a different scope, which ends at the closing brace
of main.

Here, as in the original code, there are compilers that accept it. That's
fine, so long as they complain about it. The code is broken. The original
code is also broken.
Jun 27 '08 #5
On Apr 12, 4:27 pm, Harald van Dk <true...@gmail.comwrote:
On Sat, 12 Apr 2008 02:24:13 -0700, Pradeep wrote:
Hi,
Here in the program
main()
{
extern int i; -->Global 'i'
int i=10; --->local 'i'
printf("%d\n",i);-->complier always refers to the nearest block variable
'i'(i.e., local variable 'i' which is value 10 }
so the o/p is always '10'

No, the first declaration of i is not a "global" declaration. It is a
block scope declaration of a variable. It will have to be defined
somewhere else with file scope, but that's irrelevant.

Consider this program:

int main(void) {
extern int i;
extern int f(void);

return f();

}

int f(void) {
return i;

}

int i;

This is invalid. Inside of f, the previous declaration of i is not
visible. Even though it refers to an object with file scope, the
declaration itself has a different scope, which ends at the closing brace
of main.

Here, as in the original code, there are compilers that accept it. That's
fine, so long as they complain about it. The code is broken. The original
code is also broken.
Yes, you are correct.
my assemption was wrong. i think if we check the assembly code of the
above program we can conclude some extent.
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Mark A. Gibbs | last post: by
1 post views Thread by Siddharth Jain | last post: by
5 posts views Thread by kathy | last post: by
4 posts views Thread by gaurav.dube | last post: by
3 posts views Thread by sam_cit | last post: by
10 posts views Thread by sam_cit | last post: by
10 posts views Thread by somenath | last post: by
5 posts views Thread by Philip Potter | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.