473,805 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1070
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
6202
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: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
1
2577
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 error code 5 (access denied). However, if I know the password to access the shares, how can I specify that in my program?
5
1544
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 multithread application. data in the func() must be synchonized?
0
1984
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 *Su_buffer;
4
1908
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 these .c file contain definition of one the variable declared as extern in header file. But since the header file is also included in this .c file containing definition of variable, the compiler gives a warning . The header file is common to...
3
1956
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 function, it could very well be done ny including a header file which specifies the function prototype...
10
1904
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
1914
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 bellow . Lets say I have not included stdlib.h in my program still I am using malloc so compiler will throw warring because with out prototype
5
1587
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 correctly? For example: #include <stdio.h> int main(void) { int i;
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9179
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.