473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1048
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 D©¦k <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 D©¦k <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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
1
by: Siddharth Jain | last post by:
hello I am trying to enumerate the shared folders on a server using the NetShareEnum function. Now, when the server has a password set to access the shared folders, the function returns system...
5
by: kathy | last post by:
If in one of the header file, a constant is defined: const UINT UM_CAL_DONE = func(); and it is included in several other .cpp file. Does the func() excecuted many times? If it is included in...
0
by: harsha1305 | last post by:
Hi all, I need to create a pointer to array of structure. Definition of structure: typedef struct { char b_name; unsigned long int sig; unsigned long int count; volatile unsigned char...
4
by: gaurav.dube | last post by:
I am facing a problem with warning removal I have got a header file it contains extern declarations of lot of variables (say 100) This header file is included in hundreds of .c files. Some of...
3
by: sam_cit | last post by:
Hi Everyone, I have seen in some project where functions are declared as extern, what is the possible reason to do this? To my best understanding, if some other file wan't to invoke this...
10
by: sam_cit | last post by:
Hi Everyone, I had a doubt regarding extern decleration, i tried this is one source file, extern int sample; extern int sample; int main() {
10
by: somenath | last post by:
Hi All, I have one question regarding return value cast of malloc. I learned that we should not cast the return value of malloc because it is bug hider. But my question is as mentioned...
5
by: Philip Potter | last post by:
I have a somewhat flippant question regarding undefined behaviour. Does an operation which invokes undefined behaviour affect the whole program, or are earlier statements guaranteed to execute...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.